繁体   English   中英

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

[英]When programmatically creating a new IIS web site, how can I add it to an existing 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();

< 更新 >

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

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

< / update >

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

您必须在虚拟目录(而不是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);
  }
}

请注意,如果您没有向应用程序显式添加新的虚拟目录,则metabasePath将只是“ IIS://<servername>/W3SVC/<siteID>/Root

您需要从IIS://{0}/W3SVC/AppPools获取AppPool,并将其附加到siteAppPoolId 就像是:

 var appPool = new DirectoryEntry(
     string.Format("IIS://{0}/W3SVC/AppPools/{1}", webServer, appPoolName)
 );
 site.Properties["AppPoolId"].Value = appPool;

site.Properties [“AppPoolId”] [0] =“poolname”;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM