12

我已经成功地自动化了创建新 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();

<更新>

虽然这与问题没有直接关系,但这里有一些上面使用的示例值。这可能会帮助某人更轻松地准确理解上面的代码在做什么。

  • 网络服务器:“本地主机”
  • 服务器评论:“testing.dev”
  • serverBindings:“:80:testing.dev”
  • 主目录:“c:\inetpub\wwwroot\testing\”

< /更新>

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

4

3 回答 3

6

您必须在虚拟目录(而不是网络服务器)上分配 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

于 2010-03-16T16:50:23.417 回答
0

您需要从 AppPoolfrom 中获取,IIS://{0}/W3SVC/AppPools并将其附加到site. AppPoolId就像是:

 var appPool = new DirectoryEntry(
     string.Format("IIS://{0}/W3SVC/AppPools/{1}", webServer, appPoolName)
 );
 site.Properties["AppPoolId"].Value = appPool;
于 2010-03-12T17:32:24.053 回答
0

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

于 2011-09-08T09:39:12.550 回答