during web setup deployment how to create Application pool and attach to the current application for which web setup is running

14 views Asked by At

I have a created a web setup for the web application. Now Customer wants on installation address where dropdown contains of all the application pools a new created application pool must be displayed and selected by default. So that this application in IIS under default website must be attached to this application pool.

when I was not able to customize the Installation address I created a Method and called it form Install method.

But it creates the application pool only but do not attach it to the Application.

   public override void Install(IDictionary stateSaver)
    {
        string websiteName = "PrismeTid";
        string poolName = "JamesBharatPool" ;
        bool isEnable32bit = true;
        ManagedPipelineMode mode = ManagedPipelineMode.Integrated;
       
        string runtimeVersion =  "v4.0";

        base.Install(stateSaver);

        AttachPoolToWebsite(websiteName, poolName, isEnable32bit, mode, runtimeVersion);
        //This method code is given below.
     }



   private static void AttachPoolToPrismeTid(string websiteName, string poolName,  
               bool isEnable32bit, ManagedPipelineMode mode, string runtimeVersion = "v4.0")
    {
        try
        {
            using (var serverManager = new ServerManager())
            {
                bool poolCreated = false;
                // Find the application pool
                var appPool = serverManager.ApplicationPools.FirstOrDefault(p => p.Name ==  
                                                                                 poolName);
     
                if (appPool == null)
                {
                    appPool = serverManager.ApplicationPools.Add(poolName);
                    poolCreated = true;
                }

                appPool.ManagedRuntimeVersion = runtimeVersion;
                appPool.Enable32BitAppOnWin64 = isEnable32bit;
                appPool.ManagedPipelineMode = mode;

                if (poolCreated)
                {
                    appPool.AutoStart = true;
                    appPool.ManagedPipelineMode = mode;
                    appPool.ManagedRuntimeVersion = runtimeVersion;
                    appPool.Enable32BitAppOnWin64 = isEnable32bit;
                    appPool.ProcessModel.IdentityType = ProcessModelIdentityType.LocalSystem;
                    appPool.Recycling.PeriodicRestart.Time = TimeSpan.Zero;

                    serverManager.CommitChanges();

                    appPool.Start();

                    bool isPoolInitialized = false;
                    int timeoutInSeconds = 30;
                    int elapsedSeconds = 0;
                    while (!isPoolInitialized && elapsedSeconds < timeoutInSeconds)
                    {
                        Thread.Sleep(1000);
                        appPool = serverManager.ApplicationPools.FirstOrDefault(p => p.Name == 
                        poolName);
                        if (appPool != null && appPool.State == ObjectState.Started)
                        {
                            isPoolInitialized = true;
                            break;
                        }
                        elapsedSeconds++;
                    }
                    if (!isPoolInitialized)
                    {
                       throw ex;
                    }
                   
                    var site = serverManager.Sites.FirstOrDefault(s => s.Name == "Default Web                       
                                                                  Site");
                    if (site == null)
                    {
                        throw new Exception("Default Web Site not found.");
                    }

                    var app = site.Applications.FirstOrDefault(a => a.Path == "/" + 
                     websiteName);
                    if (app == null)
                    {
                         throw ex;
                    }
                    app.ApplicationPoolName = poolName;
                }
                else
                {
                   throw ex;
                }
                serverManager.CommitChanges();
            }
        }
        catch (Exception ex)
        {
           throw ex;
        }
    }

This code works fine in the Console application. But in custom action it do not attach application pool.

0

There are 0 answers