簡體   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