代码之家  ›  专栏  ›  技术社区  ›  Ian Robinson

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

  •  12
  • Ian Robinson  · 技术社区  · 15 年前

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

    < 更新 gt;

    尽管这与问题没有直接关系,但上面使用了一些示例值。这可能有助于人们更容易理解上面的代码所做的事情。

    • webserver:“本地主机”
    • 服务器注释:“testing.dev”
    • 服务器绑定:“:80:testing.dev”
    • 主目录:“c:\ inetpub\wwwroot\testing\”

    < 更新 gt;

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

    3 回复  |  直到 6 年前
        1
  •  6
  •   Ian Robinson    15 年前

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

        2
  •  0
  •   Mark Brackett    15 年前

    你需要从 IIS://{0}/W3SVC/AppPools ,并将其附加到 site AppPoolId . 类似于:

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

    site.properties[“apppoolid”][0]=“池名称”;