在IIS 7中克隆/复制/复制现有应用程序池

时间:2012-09-28 09:24:07

标签: c# iis

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

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

现在,我想使用Microsoft.Web.Administration命名空间中的类在C#中执行相同的操作。我浏览了命名空间,但我找不到一种方法可以轻松完成。我可以调用MemberwiseClone方法来创建现有应用程序池的浅表副本,但我不知道是否会复制所有原始应用程序池属性。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分: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);
            }
        }
    }

答案 1 :(得分: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);
    }
}