5

在 PowerShell 中,可以将现有的 IIS 7 应用程序池克隆到新的应用程序池中,并在新池中保留所有源池设置。像这样...

import-module webadministration
copy IIS:\AppPools\AppPoolTemplate IIS:\AppPools\NewAppPool -force

现在我想在 C# 中使用 Microsoft.Web.Administration 命名空间中的类来做同样的事情。我已经浏览了命名空间,但我找不到轻松做到这一点的方法。我可以调用 MemberwiseClone 方法来创建现有应用程序池的浅表副本,但我不知道这是否会复制所有原始应用程序池属性。

任何人都可以帮忙吗?

4

2 回答 2

3

到目前为止,我只发现了一种在 C# 中复制应用程序池的方法:

    private void creationizeAppPoolOldSchool(string strFullName)
    {
        RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); 
        Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration); 
        runspace.Open(); 
        RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
        scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
        scriptInvoker.Invoke("import-module webadministration");
        string str = "copy IIS:\\AppPools\\_JANGO_FETT IIS:\\AppPools\\" + strFullName + " –force";
        scriptInvoker.Invoke(str);
    }

因为我真正需要的是在所有新的应用程序池上都有一组预定义的设置,所以我实际上放弃了复制现有的模板池,而是使用Microsoft.Web.Administration. 虽然这不是最初的问题,但我还是分享了它,因为浏览这篇文章的人也可能对此感兴趣:

    public static void CreateCoCPITAppPool(string strName)
    {
        using (ServerManager serverManager = new ServerManager())
        {
            ApplicationPool newPool = serverManager.ApplicationPools.Add(strName);
            newPool.ManagedRuntimeVersion = "v4.0";
            newPool.AutoStart = true;
            newPool.ProcessModel.UserName = "username";
            newPool.ProcessModel.Password = "password";
            newPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
            newPool.Recycling.PeriodicRestart.Time = TimeSpan.Zero;
            newPool.ProcessModel.IdleTimeout = TimeSpan.FromMinutes(10000); // .Zero;
            newPool.ProcessModel.ShutdownTimeLimit = TimeSpan.FromSeconds(3600);
            newPool.Failure.RapidFailProtection = false;
            serverManager.CommitChanges();
            IDictionary<string, string> attr = newPool.Recycling.RawAttributes;
            foreach (KeyValuePair<String, String> entry in attr)
            {
                // do something with entry.Value or entry.Key
                Console.WriteLine(entry.Key + " = " + entry.Value);
            }
            ConfigurationAttributeCollection coll = newPool.Recycling.Attributes;
            foreach (ConfigurationAttribute x in coll)
            {
                Console.WriteLine(x.Name + " = " + x.Value);
            }
        }
    }
于 2013-05-24T10:19:26.227 回答
1

我不确定复制方法,但您可以访问当前应用程序池的属性,然后创建一个具有相同属性的新应用程序池:

// How to access a specific app pool
DirectoryEntry appPools = new DirectoryEntry("IIS://" + serverName + "/w3svc/apppools", adminUsername, adminPassword);
foreach (DirectoryEntry AppPool in appPools.Children)
{
    if (appPoolName.Equals(AppPool.Name, StringComparison.OrdinalIgnoreCase))
    {
        // access the properties of AppPool...
    }
}

然后通过调用下面列出的方法在代码中创建一个新池:

CreateAppPool("IIS://Localhost/W3SVC/AppPools", "MyAppPool");

来自MSDN的应用程序池创建方法:

static void CreateAppPool(string metabasePath, string appPoolName)
{
    //  metabasePath is of the form "IIS://<servername>/W3SVC/AppPools"
    //    for example "IIS://localhost/W3SVC/AppPools" 
    //  appPoolName is of the form "<name>", for example, "MyAppPool"
    Console.WriteLine("\nCreating application pool named {0}/{1}:", metabasePath, appPoolName);

    try
    {
        if (metabasePath.EndsWith("/W3SVC/AppPools"))
        {
            DirectoryEntry apppools = new DirectoryEntry(metabasePath);
            DirectoryEntry newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");
            newpool.CommitChanges();
        }
        else
        {
            Console.WriteLine(" Failed in CreateAppPool; application pools can only be created in the */W3SVC/AppPools node.");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed in CreateAppPool with the following exception: \n{0}", ex.Message);
    }
}
于 2012-09-28T12:04:29.430 回答