每当重新加载App域时,如何强制IIS应用程序池重新启动?

Rob*_*use 10 asp.net iis appdomain application-pool asp.net-mvc-4

我们有一个ASP.NET MVC 4应用程序,它链接到传统的本机代码.问题是这个遗留代码具有在启动时构建的全局静态代码,但由于本机代码对App Domains一无所知,因此在重新加载App Domain时不会重新初始化该代码.在重新启动应用程序池进程之前,这会导致我们的应用程序中出现不正确的行为或崩溃.

因此,我想强制应用程序池在我们的应用程序的App Domain被回收时进行回收.IIS中是否有这样的设置,或者是否正在卸载域时我可以在我的应用程序中调用代码?

关于我的设置的一些信息,

  1. ASP.NET MVC 4应用程序
  2. IIS 7.5,但如果需要,我可以移动到8
  3. 我可以确保每个应用程序池有一个应用程序,因此我不会影响其他应用程序.

更新

根据下面的答案,我连接到AppDomain卸载事件,并使用类似于以下的代码来回收应用程序池.

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 );
}
Run Code Online (Sandbox Code Playgroud)

Mik*_*oud 2

根据您的帖子,您似乎知道何时要触发重新启动,因此这里有一篇重新启动(回收)应用程序池的帖子,它将告诉您如何操作。