3

我需要更改某个网站下所有/选定应用程序的应用程序池。我的 IIS 上有所有网站和应用程序池,但我无法更改它们。有任何想法吗?

这是我到目前为止所做的......这对我来说看起来很奇怪,因为只有字符串变化,而不是对象。

private void ChangeAppPool()
{
    Microsoft.Web.Administration.Site site = (Microsoft.Web.Administration.Site)this.websiteList.SelectedItem;
    Microsoft.Web.Administration.ApplicationPool appPool = (Microsoft.Web.Administration.ApplicationPool)this.appPoolCombo.SelectedItem;

    site.Stop();
    site.ApplicationDefaults.ApplicationPoolName = appPool.Name;

    foreach (var item in site.Applications)
    {
        item.ApplicationPoolName = appPool.Name;
    }

    site.Start();
    appPool.Recycle();
}
4

1 回答 1

9

我修改了您给定的代码以使用ServerManager,因为您的代码对我不起作用。(什么是this.websiteList.SelectedItem?将字符串转换为站点?)

ServerManager serverManager = new ServerManager();
Site site = serverManager.Sites[0]; // get site by Index or by siteName
ApplicationPool appPool = serverManager.ApplicationPools[1]; // get appPool by Index or by appPoolName

site.Stop();
site.ApplicationDefaults.ApplicationPoolName = appPool.Name;

foreach (var item in site.Applications)
{
    item.ApplicationPoolName = appPool.Name;
}

serverManager.CommitChanges();  // this one is crucial!!! see MSDN: 
// Updates made to configuration objects must be explicitly written to the configuration 
// system by using the CommitChanges method!!
site.Start();
appPool.Recycle();
于 2012-08-27T19:07:31.857 回答