When programmatically creating a new IIS web site, how can I add it to an existing application pool?

StackOverflow https://stackoverflow.com/questions/2434438

  •  19-09-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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"

OTHER TIPS

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;

site.Properties["AppPoolId"][0]= "poolname";

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top