Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 克隆/复制/复制IIS 7中的现有应用程序池_C#_Iis - Fatal编程技术网

C# 克隆/复制/复制IIS 7中的现有应用程序池

C# 克隆/复制/复制IIS 7中的现有应用程序池,c#,iis,C#,Iis,在PowerShell中,可以将现有IIS 7应用程序池克隆到新的应用程序池,并在新池中保留所有源池设置。像这样 import-module webadministration copy IIS:\AppPools\AppPoolTemplate IIS:\AppPools\NewAppPool -force 现在,我想在C#中使用Microsoft.Web.Administration命名空间中的类做同样的事情。我已经浏览了名称空间,但我找不到一种简单的方法。我可以调用MemberwiseC

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

import-module webadministration
copy IIS:\AppPools\AppPoolTemplate IIS:\AppPools\NewAppPool -force
现在,我想在C#中使用Microsoft.Web.Administration命名空间中的类做同样的事情。我已经浏览了名称空间,但我找不到一种简单的方法。我可以调用MemberwiseClone方法来创建现有应用程序池的浅层副本,但我不知道这是否会复制所有原始应用程序池属性


有人能帮忙吗?

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

// 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");
应用程序池创建方法来自:

static void CreateAppPool(字符串metabasePath,字符串appPoolName)
{
//metabasePath的格式为“IIS:///W3SVC/AppPools”
//例如“IIS://localhost/W3SVC/AppPools”
//appPoolName的格式为“”,例如,“MyAppPool”
WriteLine(“\n正在创建名为{0}/{1}:”,metabasePath,appPoolName的应用程序池);
尝试
{
if(metabasePath.EndsWith(“/W3SVC/AppPools”))
{
DirectoryEntry apppools=新的DirectoryEntry(metabasePath);
DirectoryEntry newpool=apppools.Children.Add(appPoolName,“IIsApplicationPool”);
newpool.CommitChanges();
}
其他的
{
WriteLine(“CreateAppPool中失败;只能在*/W3SVC/AppPools节点中创建应用程序池。”);
}
}
捕获(例外情况除外)
{
WriteLine(“在CreateAppPool中失败,出现以下异常:\n{0}”,例如Message);
}
}

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

因为我真正需要的是在所有新的应用程序池上都有一组预定义的设置,所以我实际上放弃了复制现有的模板池,而是使用
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);
            }
        }
    }
公共静态void CreateCopitAppPool(字符串strName)
{
使用(ServerManager=newservermanager())
{
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);/.0;
newPool.ProcessModel.ShutdownTimeLimit=TimeSpan.FromSeconds(3600);
newPool.Failure.RapidFailProtection=false;
serverManager.CommitChanges();
IDictionary attr=newPool.Recycling.rawtattributes;
foreach(属性中的KeyValuePair条目)
{
//对entry.Value或entry.Key执行某些操作
Console.WriteLine(entry.Key+“=”+entry.Value);
}
ConfigurationAttributeCollection coll=newPool.Recycling.Attributes;
foreach(配置属性x在coll中)
{
Console.WriteLine(x.Name+“=”+x.Value);
}
}
}

你有没有想过这样做?@Dan Maguire,对不起,没有,没有。到目前为止,我收到的一个答案并不能完全解决这个问题。它没有说明如何将属性从第一节中的AppPool获取到第二节中新创建的池。目前我正在考虑直接在我的C#程序中调用PowerShell代码。我还有几个星期就要解决这个问题了。一旦我做了,我会发布我选择的任何解决方案。
    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);
    }
    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);
            }
        }
    }