Silny ToJa
Silny ToJa

Reputation: 2261

Add Application Pool to iis C# using Microsoft.Web.Administration

I am trying to add Application Pool. My code:

ServerManager iis = new ServerManager();
iis.ApplicationPools.Add(new ApplicationPool() {Name= "My Pool",
AutoStart=true,ManagedRuntimeVersion="v4.0", ManagedPipelineMode=ManagedPipelineMode.Integrated });

Problem is I cant create object ApplicationPool. The reason is probably that the class it inherits has a protected internal constructor. And the error is displayed that there is no such overload of the constructor for this class.

But I think I should be able to create an object of this class somehow, because the Add method accepts this type of object. It has a second overload where it accepts a string.

enter image description here

Edit:

It works but I don't want to do it this way

iis.ApplicationPools.Add("My poll");
foreach (ApplicationPool item in applicationPool)
{
    if (item.Name == "My poll")
    {
         item.AutoStart = true;
         item.ManagedRuntimeVersion = "v4.0";
         item.ManagedPipelineMode = ManagedPipelineMode.Integrated;
         iis.CommitChanges();
    }
}

I find this:

var item = iis.ApplicationPools.Add("My poll");
item.AutoStart = true;
item.ManagedRuntimeVersion = "v4.0";
item.ManagedPipelineMode = ManagedPipelineMode.Integrated;
item.Enable32BitAppOnWin64 = true;

Upvotes: 1

Views: 1333

Answers (1)

Jalpa Panchal
Jalpa Panchal

Reputation: 12799

You could try to use the below code;

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample {

    private static void Main() {

        using(ServerManager serverManager = new ServerManager()) { 
            Configuration config = serverManager.GetApplicationHostConfiguration();

            ConfigurationSection applicationPoolsSection = config.GetSection("system.applicationHost/applicationPools");

            ConfigurationElementCollection applicationPoolsCollection = applicationPoolsSection.GetCollection();

            ConfigurationElement addElement = applicationPoolsCollection.CreateElement("add");
            addElement["name"] = @"pool1";
            addElement["managedRuntimeVersion"] = @"v4.0";
            applicationPoolsCollection.Add(addElement);

            serverManager.CommitChanges();
        }
    }
}

Upvotes: 2

Related Questions