Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 690
Ian Robinson Avatar asked Mar 12 '10 17:03

Ian Robinson


People also ask

How do you add a website to application pool?

In the Connections pane, expand Sites, and then navigate to the Web site or application you want to add to the application pool. In the General section of the Advanced Settings dialog box, click the Application Pool entry, and then click the ellipses button.

Can one web application have multiple application pool?

You can have multiple applications residing in an application pool with each of them sharing the worker process. You can have several applications share the same worker process, or, one worker process per application. Having multiple applications share the same worker process has it pros and cons.

How does application pool work in IIS?

Application pools can contain one or more worker processes. Each worker process represents work being done for a Web site, Web application, or Web service. You can create a Web garden by enabling multiple worker processes to run in a single application pool. In IIS 7 and later, each application pool uses one of two .


1 Answers

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"

like image 149
JoeBilly Avatar answered Nov 09 '22 22:11

JoeBilly