Я думаю, что здесь вам нужен пользовательский объект CacheDependency, который вызывает службу WCF каждые n секунд (где n - допустимая величина задержки) и сравнивает с предыдущим результатом.чтобы увидеть, если данные обновляются.Ваша пользовательская зависимость может затем вызвать свой метод 'NotifyDependencyChanged
, который сообщает ASP.NET, что базовые данные изменились, и кэшированный объект устарел.Существует учебное пособие по созданию пользовательского объекта CacheDependency здесь .
I думаю, ваш пользовательский 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();
}
}
}