2012-01-30 7 views
3

私のasp.netアプリケーションにデータを提供するWCFがあります。このデータをアプリケーションにキャッシュしたいが、依存関係の通知を行う準備ができているかどうかは分からない。WCF、キャッシュ通知の問題を伴うASP.NET MVC

私はこのような何か必要な詳細で

  • WCFはWCFでデータをデータ
  • クライアントは、このデータを取得し返し、突然キャッシュにそのキャッシュ
  • から
  • 今すぐクライアントのリターンデータを置くを変更されました
  • クライアントは通知(?)を受け取り、新しいデータで再度キャッシュに書き込みます

準備ができていない場合はどうすれば解決できますか?私はあなたがここで必要なものだと思う

+0

あなたが「WCFのデータがキャッシュされている」と言うとき、もしかして「WCFでデータを*変更されました*」 ? – PhilPursglove

+0

はい、申し訳ありませんが、する必要があります:変更 – reizals

+0

良い質問:) – Mhmd

答えて

2

は、WCFサービスに(Nは、待ち時間の許容量である)すべてのN秒を呼び出し、データが更新されたかどうかを確認するために、以前の結果と比較したカスタムされたCacheDependencyオブジェクトであります。カスタム依存関係は、 'NotifyDependencyChangedメソッドを呼び出すことができます。このメソッドは、ASP.NETに基底のデータが変更され、キャッシュされたオブジェクトが古くなったことを通知します。カスタムCacheDependencyオブジェクトhereの作成に関するチュートリアルがあります。

私は、カスタムされたCacheDependencyは、この(未テストコード)のようになりますと思う

/// <summary> 
/// DTO for encapsulating stuff needed to create the dependency 
/// </summary> 
public class WebServiceCacheDependencySetup 
{ 
    public object serviceClient { get; set; } 
    public string clientMethod { get; set; } 
    public int pollInterval { get; set; } 
} 

/// <summary> 
/// Generic web services cache dependency 
/// </summary> 
public class WebServiceCacheDependency : CacheDependency 
{ 
    private Timer timer; 
    private static string previousHash; 

    private object client; 
    private string clientMethod; 

    /// <summary> 
    /// Constructor for the cache dependency 
    /// </summary> 
    /// <param name="setup">Object that specifies how to create dependency and call the dependent web service method</param> 
    public WebServiceCacheDependency(WebServiceCacheDependencySetup setup) 
    { 
     // Create a timer with a specified poll interval 
     timer = new Timer(CheckDependencyCallback, this, 0, setup.pollInterval); 

     client = setup.serviceClient; 
     clientMethod = setup.clientMethod; 

     previousHash = string.Empty; 
    } 

    public void CheckDependencyCallback(object sender) 
    { 
     // Reflect on the service's proxy to find the method we want to call 
     Type clientType = client.GetType(); 
     MethodInfo method = clientType.GetMethod(clientMethod); 

     // Programmatically invoke the method and get the return value 
     object returnValue = method.Invoke(client, null); 

     // Cast the return to a byte array so we can hash it 
     Byte[] returnBytes = (Byte[])returnValue; 

     using (SHA512Managed hashAlgorithm = new SHA512Managed()) 
     { 
      // Hash the return value into a string 
      Byte[] hashedBytes = hashAlgorithm.ComputeHash(returnBytes); 
      string hashedValue = Convert.ToBase64String(hashedBytes); 

      // Compare the new hash to the last hash 
      if (hashedValue != previousHash) 
      { 
       // If the hashes don't match then the web service result has changed 
       // so invalidate the cached object 
       NotifyDependencyChanged(this, EventArgs.Empty); 

       // Tear down this instance 
       timer.Dispose(); 
      } 
     } 

    } 

    protected override void DependencyDispose() 
    { 
     if (timer != null) 
     { 
      timer.Dispose(); 
     } 
    } 
} 
+0

あなたの答えは非常に便利です! :) どうも – reizals

関連する問題