添加失败通知回拨(Windows Server AppFabric 缓存)

启用缓存的应用程序可以使用 Windows Server AppFabric 接收缓存通知。本主题介绍了如何向应用程序添加失败通知回调。当缓存客户端丢失一个或多个缓存通知时将触发失败通知。有关缓存通知的常规详细信息,请参阅缓存通知(Windows Server AppFabric 缓存)

与为缓存通知添加回调类似,为失败通知添加回调包含两个步骤。首先,创建一个在触发失败通知时调用的方法。失败通知调用的方法必须接受与 DataCacheFailureNotificationCallback 委派相同的参数。第二,使用 AddFailureNotificationCallback 方法为失败通知添加回调。为 failureDelegate 参数使用第一步中创建的方法的名称。

备注

对于要使用通知的应用程序,需要在命名缓存上启用它们。将 notificationsEnabled 参数与 New-CacheSet-CacheConfig 命令一起使用。有关详细信息,请参阅使用 Windows PowerShell 管理 Windows Server AppFabric 缓存功能

添加失败通知回调的步骤

  1. 创建让失败通知触发的方法。确保失败通知调用的方法接受与 DataCacheFailureNotificationCallback 委托相同的参数。

  2. 使用 AddFailureNotificationCallback 方法为失败通知添加回调。在 failureDelegate 参数中使用失败通知发生时要调用的方法的名称。

示例

添加失败通知回调时,第一个步骤是创建一个让通知调用的方法。通知调用的方法必须接受与 DataCacheFailureNotificationCallback 委托相同的参数。此例演示了一个失败通知可调用的方法示例。

'method invoked by failure notification "ndNotificationFailure"
Public Sub myNotificationFailureDelegate(ByVal myCacheName As String, _
    ByVal nd As DataCacheNotificationDescriptor)

    Console.WriteLine("===============================")
    Console.WriteLine("Invoked by failure notification")
    Console.WriteLine("===============================")
End Sub
//method invoked by failure notification "ndNotificationFailure"
public void myNotificationFailureDelegate(string myCacheName,
    DataCacheNotificationDescriptor nd)
{
    Console.WriteLine("===============================");
    Console.WriteLine(" Invoked by failure notification");
    Console.WriteLine("===============================");
}

第二个步骤是为失败通知添加回调。在此示例中,创建了一个失败通知来调用前一个示例中的方法。

'add failure notification callback
Dim ndNotificationFailure As DataCacheNotificationDescriptor
ndNotificationFailure = _
    myTestCache.AddFailureNotificationCallback(AddressOf myNotificationFailureDelegate)
//add failure notification callback
DataCacheNotificationDescriptor ndNotificationFailure
    = myTestCache.AddFailureNotificationCallback(myNotificationFailureDelegate);

另请参阅

概念

添加缓存通知回拨(Windows Server AppFabric 缓存功能)
删除缓存通知回拨(Windows Server AppFabric 缓存)
缓存通知方法(Windows Server AppFabric 缓存)
使用 Windows PowerShell 管理 Windows Server AppFabric 缓存功能

  2011-12-05