locked
How to stop an application RRS feed

  • Question

  • User-1665794747 posted

    I have an application where each user's interaction with the site takes many pages and lasts up to 10 minutes. It runs 24/7 in its own apppool.

    As the user progresses through the pages, I store the data they provide in the session state.

    I have disabled automatic recycling to avoid the session state getting destroyed mid-interaction, and I've tried to schedule a recycle at a specific time (4am on Tuesdays) with a timer in the Application.Start event. I've tried many approaches from different forums - currently HttpRuntime.UnloadAppDomain(). The Application.OnEnd and  Application.Start events fire and the code runs, but the "old" version of the app seems to continue running. Eventually the site goes non-responsive.

    How can I programatically cause the app to stop and then restart as if the application pool had recycled?

    Tuesday, January 12, 2016 9:12 AM

Answers

  • User-698989805 posted

    If you want to set duration in the app pool, this would be recommended and hopefully helpful too:

    https://www.iis.net/configreference/system.applicationhost/applicationpools/add/recycling?_e_pi_=7%2CPAGE_ID10%2C6936134746

    And to do it programmatically, follow the below links:

    http://www.codeproject.com/Articles/26430/Recycling-IIS-application-pools-programmatical

    http://www.kunal-chowdhury.com/2013/08/how-to-manage-iis-application-pool.html

    http://weblog.west-wind.com/posts/2007/Jan/17/Programmatically-listing-getting-and-setting-Application-Pools

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 13, 2016 3:10 AM
  • User-2057865890 posted

    Hi,iLens

    The following code examples add an application pool named Contoso to IIS 7 server, then set the application pool to daily recycle at 3:00 A.M.

       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"] = @"Contoso";
             ConfigurationElement recyclingElement = addElement.GetChildElement("recycling");
             ConfigurationElement periodicRestartElement = recyclingElement.GetChildElement("periodicRestart");
             ConfigurationElementCollection scheduleCollection = periodicRestartElement.GetCollection("schedule");
             ConfigurationElement addElement1 = scheduleCollection.CreateElement("add");
             addElement1["value"] = TimeSpan.Parse("03:00:00");
             scheduleCollection.Add(addElement1);
             applicationPoolsCollection.Add(addElement);
             serverManager.CommitChanges();
          }
       }
    

    More information, please see here.

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 13, 2016 8:46 AM

All replies

  • User-698989805 posted

    If you want to set duration in the app pool, this would be recommended and hopefully helpful too:

    https://www.iis.net/configreference/system.applicationhost/applicationpools/add/recycling?_e_pi_=7%2CPAGE_ID10%2C6936134746

    And to do it programmatically, follow the below links:

    http://www.codeproject.com/Articles/26430/Recycling-IIS-application-pools-programmatical

    http://www.kunal-chowdhury.com/2013/08/how-to-manage-iis-application-pool.html

    http://weblog.west-wind.com/posts/2007/Jan/17/Programmatically-listing-getting-and-setting-Application-Pools

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 13, 2016 3:10 AM
  • User-2057865890 posted

    Hi,iLens

    The following code examples add an application pool named Contoso to IIS 7 server, then set the application pool to daily recycle at 3:00 A.M.

       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"] = @"Contoso";
             ConfigurationElement recyclingElement = addElement.GetChildElement("recycling");
             ConfigurationElement periodicRestartElement = recyclingElement.GetChildElement("periodicRestart");
             ConfigurationElementCollection scheduleCollection = periodicRestartElement.GetCollection("schedule");
             ConfigurationElement addElement1 = scheduleCollection.CreateElement("add");
             addElement1["value"] = TimeSpan.Parse("03:00:00");
             scheduleCollection.Add(addElement1);
             applicationPoolsCollection.Add(addElement);
             serverManager.CommitChanges();
          }
       }
    

    More information, please see here.

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 13, 2016 8:46 AM