views:

436

answers:

3

I have successfully automated the process of creating a new IIS website, however the code I've written doesn't care about application pools, it just gets added to DefaultAppPool. However I'd like to add this newly created site to an existing application pool.

Here is the code I'm using to create the new website.

        var w3Svc = new DirectoryEntry(string.Format("IIS://{0}/w3svc", webserver));
        var newsite = new object[] { serverComment, new object[] { serverBindings }, homeDirectory };
        var websiteId = w3Svc.Invoke("CreateNewSite", newsite);
        site.Invoke("Start", null);
        site.CommitChanges();

<update>

Although this is not directly related to the question, here are some sample values being used above. This might help someone understand exactly what the code above is doing more easily.

  • webServer: "localhost"
  • serverComment: "testing.dev"
  • serverBindings: ":80:testing.dev"
  • homeDirectory: "c:\inetpub\wwwroot\testing\"

</update>

If I know the name of the application pool that I'd like this web site to be in, how can I find it and add this site to it?

A: 

You need to get the AppPoolfrom IIS://{0}/W3SVC/AppPools, and attach it to the site's AppPoolId. Something like:

 var appPool = new DirectoryEntry(
     string.Format("IIS://{0}/W3SVC/AppPools/{1}", webServer, appPoolName)
 );
 site.Properties["AppPoolId"].Value = appPool;
Mark Brackett
One thing I was looking for was an "AppPoolId" Property on appPool, but there doesn't seem to be one. Can you shed some light on why we're assigning the appPool (type DirectoryEntry) directly to the AppPoolId Value?
Ian Robinson
@Ian - Actually, I might be incorrect there. You may only need to assign the appPoolName (the string) to AppPoolId.
Mark Brackett
Hm, well that doesn't error out, but the website is still in the default app pool. I wonder if something else needs to be set in addition to the AppPoolId?
Ian Robinson
+4  A: 

You have to assign the AppPool on the virtual dir (not the webserver) and set the AppIsolated property to 2 which mean pooled-process ;)

http://msdn.microsoft.com/en-us/library/ms525598%28v=VS.90%29.aspx

Relevant code sample from link:

static void AssignVDirToAppPool(string metabasePath, string appPoolName)
{
  //  metabasePath is of the form "IIS://<servername>/W3SVC/<siteID>/Root[/<vDir>]"
  //    for example "IIS://localhost/W3SVC/1/Root/MyVDir" 
  //  appPoolName is of the form "<name>", for example, "MyAppPool"
  Console.WriteLine("\nAssigning application {0} to the application pool named {1}:", metabasePath, appPoolName);

  try
  {
    DirectoryEntry vDir = new DirectoryEntry(metabasePath);
    string className = vDir.SchemaClassName.ToString();
    if (className.EndsWith("VirtualDir"))
    {
      object[] param = { 0, appPoolName, true };
      vDir.Invoke("AppCreate3", param);
      vDir.Properties["AppIsolated"][0] = "2";
      Console.WriteLine(" Done.");
    }
    else
      Console.WriteLine(" Failed in AssignVDirToAppPool; only virtual directories can be assigned to application pools");
  }
  catch (Exception ex)
  {
    Console.WriteLine("Failed in AssignVDirToAppPool with the following exception: \n{0}", ex.Message);
  }
}

Note that if you are not explicitly adding a new virtual directory to the application, the metabasePath will simply be "IIS://<servername>/W3SVC/<siteID>/Root"

JoeBilly
Thanks for the link - I had tried this approach but when I read your answer I realized I was never sending in the right metabasePath (to the root virtual directory for the application)! Its all good now. Thanks again.
Ian Robinson
Np. Actually IIS logic is based on virtual dirs. Almost all "web site" usage must be done on the Root virtual dir (IISVirtualDir) and not on the webserver (IISWebServer). If you can try to use WMI (DCOM required) instead of ADSI. Its more flexible (specially about permission).In IIS 7, we finally have the Microsoft.Web.Administration ;)
JoeBilly
A: 

I have been trying to get the code you provided above to work but I am totally new to this type of thing any way you can give me a hand getting this to work.

I think my problem is that i can not get the correct information that i am needing to put into the code.

* serverComment: "testing.dev"  Im not sure what this one is doing at all.
* serverBindings: ":80:testing.dev" This one looks like the end of a url string.
* homeDirectory: "c:\inetpub\wwwroot\testing\" Path to the web root right?
  • Edit -

Found the information. Now i just need to make it add the sub-site to the correct site.

Michael