locked
Reassign IIS Application Pool RRS feed

  • Question

  • User1430228758 posted

    I have code to reassign an IIS Web Application's Application Pool to another Application Pool. The Application Pool was originally set to the "DefaultAppPool" of the Default Web Site. This new Application Pool I'm wanting to change is a new Web Application created under the Default Web Site and is created from the execution of a Web Setup Project in Visual Studio 2013.  My IIS server is IIS v7.5.  Below is the code to change the new Applications Application Pool

    private void AssignAppPool(string appPoolName, string appPath) {
    
        ServerManager serverManager = new ServerManager();
        Site site = serverManager.Sites[0];
    
            ApplicationPool appPool = serverManager.ApplicationPools[appPoolName];
            site.Stop();
            foreach (var item in site.Applications)
            {
                if(item.Path == appPath)
                {
                    item.ApplicationPoolName = appPool.Name;
                    break;
                }               
            }
    
            serverManager.CommitChanges();  
            site.Start();
            appPool.Recycle();
      }

    This method is called in a Custom Actions Installer class from the overriden method Install(). So the problem is when the Web Setup program finishes, the Application pool reverts back to the DefaultAppPool that it was originally assigned to when the WebSetup.msi completed.

    I can step through the Custom Actions (Installer.cs) code and see the Applications being assigned to the "new" Application pool. But as mentioned, once the WebSetup.msi completes, the Application Pool changed, reverts back to DefaultAppPool

    Wednesday, May 18, 2016 11:18 AM

All replies

  • User-166373564 posted

    Hi,

    I can step through the Custom Actions (Installer.cs) code and see the Applications being assigned to the "new" Application pool. But as mentioned, once the WebSetup.msi completes, the Application Pool changed, reverts back to DefaultAppPool

    According to your code snippet above, it seems that your code is ok, and updates are made to configuration objects using the CommitChanges method.

    Could you provide more background information about this issue? we will help look into it further.

    Regards,

    Angie

    Thursday, May 19, 2016 6:08 AM
  • User1430228758 posted

    I'm using Visual Studio 2013. I have a Solution that has several Web ASP.NET Web Application projects and a Visual Studio Installer ("Web Setup")  project along with a Class Library project that's used provide custom actions upon installation of the web sites. Custom actions such as create IIS Application pools, assign the new Web Applications to Application Pools, modify values in the Web.config files etc and so forth. 

    The Web Setup project is configured to create several new Web Applications and their Virtual directories on the target IIS web server.  And that all works. Meaning, all the Web Applications with their Virtual directories are created. The new Web Applications are created Under the "Default Web Site" Site which is using the "DefaultApppPool".  

    The custom action code (via Installer.cs) fires off as expected, and creates several new IIS Web Application Pools.  I want to assign the newly created Web Applications (under the Default Web Site) to the newly created Application Pools just mentioned. The code I provided above is suppose to do that. And as mentioned, I can debug the code in the Installer execution, and watch as the newly created Web Applications are assigned to the newly created Application Pools. I can see their re-assignments while using IIS Manager.

    However, once the Web Setup Project completes, hence the Installer Class is done doing it's stuff,  and I  go back to IIS Manager, those Web Applications that had their Application Pool assignments changed through the Installer.cs code, revert back to their original Application Pool assignment "DefaultAppPool".  It's as though the Installer code never ran. 

    Update:  I just created a new Console project with the same code that's in my Installer.cs class that re-assigns the Web Applications Application Pools, as I've described above. Everything works as expected and the Newly created Web Applications Application Pools, don't revert back to the "DefaultAppPool" Application Pool as it does in the Web Setup / Custom Installer.cs code.  So there's must be something going with the execution of the Web Setup / Installer projects.

    Thursday, May 19, 2016 10:02 AM