Windows – Microsoft.Web.Administration and app pool does not set

iis-7windows

I have created a nice custom installer in VS2010 which not only installs the website to IIS but also does some custom actions like registering COM Dll's and other things. In the process I am also using Microsoft.Web.Administration to do the following:

  • Create a custom app pool for the website, with specified .NET Framework version, pipeline mode etc…
  • Assign the app pool created to the installed website.

The problem I have is that when I do this:

sm.Sites[0].Applications["/MyNewSite"].ApplicationPoolName = newPool.Name;
sm.CommitChanges();

It appears in code to be assigned and working (i.e no errors) but after the installation, I open up the IIS manager and look at the web application app pool – the app pool is certainly not what I assigned it in code at all. It is just the default app pool however the app pool I created in code is definitely there, with the right details and information.

I DO NOT want to change the website app pool, but the web application. (i.e default website > MyNewSite app pool, NOT /default website)

I have been stumbled by this for over a day and cannot think why it does not work.

what am I doing wrong?

I am installing on a WS2008R2 + all updates box. IIS 7.5.

Thanks

Best Answer

You can try the following code, as created by IIS Manager:

 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 sitesSection = config.GetSection("system.applicationHost/sites");

             ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();

             ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"MySite");
             if (siteElement == null) throw new InvalidOperationException("Element not found!");


             ConfigurationElementCollection siteCollection = siteElement.GetCollection();

             ConfigurationElement applicationElement = FindElement(siteCollection, "application", "path", @"/subsite");
             if (applicationElement == null) throw new InvalidOperationException("Element not found!");

             applicationElement["applicationPool"] = @"TheAppPool";

             serverManager.CommitChanges();
         }
     }

     private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues) {
         foreach (ConfigurationElement element in collection) {
             if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase)) {
                 bool matches = true;

                 for (int i = 0; i < keyValues.Length; i += 2) {
                     object o = element.GetAttributeValue(keyValues[i]);
                     string value = null;
                     if (o != null) {
                         value = o.ToString();
                     }

                     if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase)) {
                         matches = false;
                         break;
                     }
                 }
                 if (matches) {
                     return element;
                 }
             }
         }
         return null;
     }
 }