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

发布于 2024-08-24 13:12:12 字数 843 浏览 5 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春庭雪 2024-08-31 13:12:12

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

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:///W3SVC/”。 siteID>/Root"

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"

一梦等七年七年为一梦 2024-08-31 13:12:12

您需要从 IIS://{0}/W3SVC/AppPools 获取 AppPool,并将其附加到 siteAppPoolId。像这样的东西:

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

You need to get the AppPoolfrom IIS://{0}/W3SVC/AppPools, and attach it to the site's AppPoolId. Something like:

 var appPool = new DirectoryEntry(
     string.Format("IIS://{0}/W3SVC/AppPools/{1}", webServer, appPoolName)
 );
 site.Properties["AppPoolId"].Value = appPool;
烟雨扶苏 2024-08-31 13:12:12

site.Properties["AppPoolId"][0]= "池名称";

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文