以编程方式更改应用程序的AppPool [英] Change AppPool of Application programmatically

查看:62
本文介绍了以编程方式更改应用程序的AppPool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改某个网站下所有/选定应用程序的应用程序池. 我在IIS上获得了所有网站和应用程序池,但是无法更改它们. 有什么想法吗?

I need to change the app pool of all/selected applications under a certain website. I got all websites and App Pools on my IIS, but I can't change them. Any ideas?

这是我到目前为止所做的... 在我看来,这很奇怪,因为只更改了字符串,没有对象.

Here's what I've done so far... It looks strange to me, because there's only string changing, not object.

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();
}

推荐答案

我修改了给定的代码,以使用

I modified your given code to use ServerManager class as your code did not work for me. (what is this.websiteList.SelectedItem? cast string to Site?)

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();

这篇关于以编程方式更改应用程序的AppPool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆