我们有一个ASP.NET MVC 4应用程序,链接到遗留的本地代码。问题在于这些遗留代码有全局静态变量,在启动时构造,但是由于本地代码对应用程序域一无所知,因此当应用程序域重新加载时,该代码不会被重新初始化。这导致我们的应用程序出现不正确的行为或崩溃,直到应用程序池进程重启。
因此,我想在我们的应用程序的应用程序域被回收时强制应用程序池进行回收。在IIS中是否有此设置,或者在卸载域时是否可以在我的应用程序中调用代码?
一些关于我的设置的信息:
1. ASP.NET MVC 4应用程序 2. IIS 7.5,但如果需要,我可以移动到8 3. 我可以确保每个应用程序池只有一个应用程序,因此不会影响其他应用程序。
更新:
根据下面的答案,我连接到了AppDomain卸载事件,并使用类似于以下内容的代码来回收应用程序池。
因此,我想在我们的应用程序的应用程序域被回收时强制应用程序池进行回收。在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 );
}