asp.net - 每当重新加载应用程序域时,您如何强制 IIS 应用程序池重新启动?

标签 asp.net iis asp.net-mvc-4 appdomain application-pool

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

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

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

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

  • 更新

    根据下面的答案,我连接到 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 );
    }
    

    最佳答案

    根据您的帖子,您似乎知道何时要触发重启,所以这里是 Restarting (Recycling) an Application Pool post那会告诉你如何。

    关于asp.net - 每当重新加载应用程序域时,您如何强制 IIS 应用程序池重新启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11848595/

    相关文章:

    asp.net-mvc-4 - 修复!类型 'ASP._Page__ViewStart_cshtml' 不继承自 'System.Web.WebPages.StartPage'

    javascript - 如何在刷新时更改 Bootstrap 表属性

    c# - ASP.Net Core API 访问 HttpRequestMessage

    sql - Azure 网站无法连接到 SQL Azure 数据库

    c# - aspx page.pls的URL重写给我建议

    asp.net - 如何在 Windows 10/IIS 10 上附加到 IIS 进程 (w3wp.exe)?

    azure - Windows Azure 中的 ERROR_DESTINATION_INVALID

    asp.net - 在 IIS 中托管时不应用字体

    asp.net - 如何在使用 MasterPage 的 ASP.NET Web 窗体中为服务器控件设置特定 ID?

    IIS 7 Url Rewriting 和两个域