2012-03-20 3 views
3

を再起動します。クリア一時ASP.NETファイル我々は動的に起動時にアセンブリをロードし、参照としてそれらを追加している

BuildManager.AddReferencedAssembly(assembly); 

アプリケーションは、実行時に新しいプラグインをインストールすることができます。インストール/アンインストールの後、Webアプリケーションを再起動します。私が試した:

HostingEnvironment.InitiateShutdown(); 

System.Web.HttpRuntime.UnloadAppDomain(); 

をしかし、新しいバージョンのプラグインがロードされていない - 私は、これはASP.NETが積極的に参照されるアセンブリをキャッシュする方法が原因であると考えている - 特にASP .NET MVCコントローラ。

これは問題ではありません。これは、プラグインアセンブリのバージョンが毎回増分されるためです。しかし、開発時には、プラグインを少し変更するたびにバージョン番号を変更したくないので、これはもっと問題になります。

私たちは、temp asp.netファイルの消去を、プログラム的にまたはポストビルドイベントを使用して強制できますか?

解決策の1つはglobal.asaxに触れることですが、これは私にとってはちょっとハッキリです。

+0

私たちは似た何かをしたし、最終的に落ち着い:http://stackoverflow.com/questions/407713/how-to-restart-asp-net-application-besides-modifying-web-config – dash

答えて

1

次のコードを使用してオンデマンドでアプリケーションプールをリセットしました。 (これをコントローラアクションに接続するだけです)。

注:これはアプリケーションプールなので、同じアプリケーションプールで実行されている他のアプリへの影響を確認したい場合があります。

public class IisManager 
{ 
    public static string GetCurrentApplicationPoolId() 
    { 
     // Application is not hosted on IIS 
     if (!AppDomain.CurrentDomain.FriendlyName.StartsWith("/LM/")) 
      return string.Empty; 
     // Application hosted on IIS that doesn't support App Pools, like 5.1 
     else if (!DirectoryEntry.Exists("IIS://Localhost/W3SVC/AppPools")) 
      return string.Empty; 

     string virtualDirPath = AppDomain.CurrentDomain.FriendlyName; 
     virtualDirPath = virtualDirPath.Substring(4); 
     int index = virtualDirPath.Length + 1; 
     index = virtualDirPath.LastIndexOf("-", index - 1, index - 1); 
     index = virtualDirPath.LastIndexOf("-", index - 1, index - 1); 
     virtualDirPath = "IIS://localhost/" + virtualDirPath.Remove(index); 
     var virtualDirEntry = new DirectoryEntry(virtualDirPath); 
     return virtualDirEntry.Properties["AppPoolId"].Value.ToString(); 
    } 


    public static void RecycleApplicationPool(string appPoolId) 
    { 
     string appPoolPath = "IIS://localhost/W3SVC/AppPools/" + appPoolId; 
     var appPoolEntry = new DirectoryEntry(appPoolPath); 
     appPoolEntry.Invoke("Recycle"); 
    } 

    public static void RecycleApplicationPool(string appPoolId, string username, string password) 
    { 
     string appPoolPath = "IIS://localhost/W3SVC/AppPools/" + appPoolId; 
     var appPoolEntry = new DirectoryEntry(appPoolPath, username, password); 
     appPoolEntry.Invoke("Recycle"); 
    } 

} 

オーバーライドされたメソッドは、明示的にIISのインスタンスをホストするマシン/サーバ上で管理者権限を持つユーザーを渡したいのインスタンスに対応するためです。

コントローラの動作は次のようなものになります。

public string ResetAppPool() 
    { 
     var appPoolId = IisManager.GetCurrentApplicationPoolId(); 
     if (appPoolId.Equals(string.Empty)) 
      return "Application is not running inside an App Pool"; //May be not IIS 6 onwards 
     try 
     { 
      IisManager.RecycleApplicationPool(appPoolId); //Can only be used by Admin users 
      return string.Format("App pool {0} recycled successfully", appPoolId); 
     } 
     catch (Exception ex) 
     { 
      Logger.Error("Failed to recycle app pool : " + ex.StackTrace); 
      return string.Format("App pool {0} recycle failed", appPoolId); 
     } 
    } 
関連する問題