は、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();
}
}
}
あなたが「WCFのデータがキャッシュされている」と言うとき、もしかして「WCFでデータを*変更されました*」 ? – PhilPursglove
はい、申し訳ありませんが、する必要があります:変更 – reizals
良い質問:) – Mhmd