従来のネイティブ コードにリンクする ASP.NET MVC 4 アプリケーションがあります。問題は、このレガシ コードには起動時に構築されるグローバルな静的情報があることですが、ネイティブ コードはアプリ ドメインについて何も知らないため、アプリ ドメインがリロードされたときにそのコードが再初期化されません。これにより、アプリケーション プール プロセスが再起動されるまで、アプリで不適切な動作やクラッシュが発生します。
このため、アプリケーションのアプリ ドメインがリサイクルされるたびに、アプリケーション プールを強制的にリサイクルしたいと考えています。このための IIS の設定はありますか、またはドメインがアンロードされているときにアプリケーションで呼び出すことができるコードはありますか?
私のセットアップに関するいくつかの情報、
- ASP.NET MVC 4 アプリケーション
- IIS 7.5 ですが、必要に応じて 8 に移行できます
- アプリケーション プールごとに 1 つのアプリケーションが存在することを確認できるため、他のアプリケーションに影響を与えることはありません。
アップデート
以下の回答に基づいて、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 );
}