更改应用程序池的属性

时间:2011-10-28 23:52:02

标签: c# iis-7 application-pool

有没有办法让这段代码更好?

ServerManager serverManager = new ServerManager();
            ApplicationPoolCollection applicationPoolCollection = serverManager.ApplicationPools;

            foreach (var site in serverManager.Sites)
            {

                //Trace.WriteLine("Site: {0}", site.Name);)
                foreach (var app in site.Applications)
                {

                    if (app.Path.EndsWith("Report") || app.Path.EndsWith("Data"))
                    {
                        Trace.WriteLine("ReportApi     "  + app.ApplicationPoolName);

                        applicationPoolCollection[app.ApplicationPoolName].Enable32BitAppOnWin64 = true;
                        applicationPoolCollection[app.ApplicationPoolName].ManagedRuntimeVersion = "v4.0";


                    }
                }
            }

            serverManager.CommitChanges();

第二个问题,如果我将在只有iis6的PC上运行它将会发生什么

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

对我来说看起来很整洁。如果你想稍微讨论一下,你可以使用Cast扩展方法(对IEnumerable的类型进行操作,将它们转换为IEnumerable<T>)到 make事情更LINQy。它使某些东西更清晰,对其他东西更不透明。随便挑选。

var reportOrDataApps = serverManager
    .Sites
    .Cast<Site>()
    .SelectMany(s => s.Applications.Cast<Application>())
    .Where(app => app.Path.EndsWith("Report") || app.Path.EndsWith("Data"))

foreach(var app in reportOrDataApps)
{
    Trace.WriteLine("ReportApi     "  + app.ApplicationPoolName);
    applicationPoolCollection[app.ApplicationPoolName].Enable32BitAppOnWin64 = true;
    applicationPoolCollection[app.ApplicationPoolName].ManagedRuntimeVersion = "v4.0";
}

编辑:

看来这些集合已经实现了IEnumerable<T>,因此您可以缩短LINQ:

var reportOrDataApps = serverManager
    .Sites
    .SelectMany(s => s.Applications)
    .Where(app => app.Path.EndsWith("Report") || app.Path.EndsWith("Data"))