Esempio n. 1
0
        private static string ChangeApplicationPool(IIisManager iisManager, long siteId, string oldPool, bool oldPoolCreated, string prospectivePoolName, string defaultPoolName)
        {
            string newPool = null;

            if (iisManager.IsApplicationPoolSupported)
            {
                bool newPoolCreated = false;
                bool poolChanged = false;

                try
                {
                    // Create new pool
                    if (string.IsNullOrEmpty(prospectivePoolName))
                    {
                        string[] pools = iisManager.ListApplicationPools();
                        newPool = BuildUniqueName(defaultPoolName, pools);
                        iisManager.CreateApplicationPool(newPool, true);
                        newPoolCreated = true;
                    }
                    else
                        newPool = prospectivePoolName;

                    // Change pool
                    iisManager.ChangeApplicationPool(siteId, newPool);
                    poolChanged = true;

                    // Delete old pool
                    if (oldPoolCreated)
                    {
                        try
                        {
                            iisManager.DeleteApplicationPool(oldPool);
                        }
                        catch (Exception ex)
                        {
                            Log.WriteException("ChangeApplicationPool", ex);
                        }
                    }
                }
                catch
                {
                    #region Undo changes

                    try
                    {
                        if (poolChanged)
                            iisManager.ChangeApplicationPool(siteId, oldPool);
                    }
                    catch
                    {
                    }

                    try
                    {
                        if (newPoolCreated)
                            iisManager.DeleteApplicationPool(newPool);
                    }
                    catch
                    {
                    }

                    #endregion

                    throw;
                }
            }

            return newPool;
        }