以编程方式创建新的IIS网站时,如何将其添加到现有的应用程序池中?

Ian*_*son 12 c# iis application-pool

我已经成功地自动创建了一个新的IIS网站,但是我编写的代码并不关心应用程序池,它只是添加到DefaultAppPool中.但是,我想将这个新创建的站点添加到现有的应用程序池中.

这是我用来创建新网站的代码.

        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();
Run Code Online (Sandbox Code Playgroud)

< 更新 >

虽然这与问题没有直接关系,但以下是上面使用的一些示例值.这可能有助于人们更准确地理解上面代码的作用.

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

< / update >

如果我知道我希望此网站所在的应用程序池的名称,我该如何找到它并将其添加到该网站?

Joe*_*lly 6

您必须在虚拟目录(而不是Web服务器)上分配AppPool,并将AppIsolated属性设置为2,这意味着池化进程;)

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

来自链接的相关代码示例:

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果您没有明确地向应用程序添加新的虚拟目录,那么metabasePath将只是" IIS://<servername>/W3SVC/<siteID>/Root"