1

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;
1
  • "I don't want" is never a valid excuse. Commented Apr 23, 2020 at 16:11

1 Answer 1

2

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I found another solution, but that's also fine.
@SilnyToJa If your issue is solved then I request you to mark the helpful suggestion as an answer. This will help other people who face the same issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.