How do you force the IIS Application Pool to restart whenever the App Domain is reloaded?

14,677

Solution 1

Based on your post it appears you know when you want to trigger the restart so here is a Restarting (Recycling) an Application Pool post that will tell you how.

Solution 2

A more brutal approach is to call Process.GetCurrentProcess().Kill() Not very graceful, but if your site has its own app pool and you don't care any current requests being brutally stopped, that's quite effective!

Solution 3

A simplified VB version of the code you shared. This version uses a For loop instead of a LINQ query. Also, in order to use Microsoft.Web.Administration, you must import the DLL from c:\windows\system32\inetsrv

Imports System.Diagnostics
Imports Microsoft.Web.Administration

Dim pid As Integer = Process.GetCurrentProcess().Id
Dim manager = New ServerManager()
For Each p As WorkerProcess In manager.WorkerProcesses
    If p.ProcessId = pid Then
         For Each a As ApplicationPool In manager.ApplicationPools
             If a.Name = p.AppPoolName Then
                 a.Recycle()
                 Exit For
             End If
         Next
         Exit For
    End If
Next
Share:
14,677
Rob Prouse
Author by

Rob Prouse

Proven software engineer and architect with a passion for building high performance agile teams and empowering them to deliver customer value through software and technology. Recognized by Microsoft as a Most Valuable Professional (MVP) for thought leadership in C#, .NET and DevOps. Head of the opensource NUnit Test Framework development team, a member of the .NET Foundation and a frequent contributor to numerous opensource projects. My opensource packages have been downloaded over 300 million times.

Updated on September 21, 2022

Comments

  • Rob Prouse
    Rob Prouse over 1 year

    We have an ASP.NET MVC 4 application that links to legacy native code. The problem is that this legacy code has global statics that are constructed at startup, but because native code knows nothing about App Domains, that code is not re-initialized when the App Domain is reloaded. This causes incorrect behaviour or crashes in our app until the Application Pool process is restarted.

    Because of this, I would like to force the Application Pool to recycle whenever our application's App Domain is recycled. Is there a setting in IIS for this, or is there code that I can call in my application as the domain is being unloaded?

    Some info on my setup,

    1. ASP.NET MVC 4 application
    2. IIS 7.5, but I can move to 8 if required
    3. I can ensure that there is one application per Application Pool, so I will not be affecting other applications.

    Update

    Based on the answer below, I hooked up to the AppDomain unload event and used code similar to the following to recycle the Application Pool.

    try
    {
       // Find the worker process running us and from that our AppPool
       int pid = Process.GetCurrentProcess().Id;
       var manager = new ServerManager();
       WorkerProcess process = (from p in manager.WorkerProcesses where p.ProcessId == pid select p).FirstOrDefault();
    
       // From the name, find the AppPool and recycle it
       if ( process != null )
       {
          ApplicationPool pool = (from p in manager.ApplicationPools where p.Name == process.AppPoolName select p).FirstOrDefault();
          if ( pool != null )
          {
             log.Info( "Recycling Application Pool " + pool.Name );
             pool.Recycle();
          }
       }
    }
    catch ( NotImplementedException nie )
    {
       log.InfoException( "Server Management functions are not implemented. We are likely running under IIS Express. Shutting down server.", nie );
       Environment.Exit( 0 );
    }
    
  • Rob Prouse
    Rob Prouse over 11 years
    Thanks, that wasn't exactly what I needed, but it pointed me in the right direction.
  • Abhishek
    Abhishek over 7 years
    @RobProuse so what did you do then?
  • Rob Prouse
    Rob Prouse over 7 years
    George, I put the code that I wrote in my update above in the handler for the AppDomain unload event that I register at startup. In the end though, I converted our code to a self hosted ASP.NET app so that we could manage the lifecycle of the app and AppDomains.