public async Task RegisterServiceNotification(string serviceNameUri)
{
var notifications = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, long>>("ServiceNotifications");
using (var tx = this.StateManager.CreateTransaction())
{
await notifications.GetOrAddAsync(tx, serviceNameUri, (serviceName) =>
{
logger.LogInformation("Registering notification filter for {serviceName}", serviceName);
var filterDescription = new ServiceNotificationFilterDescription
{
Name = new Uri(serviceName)
};
return fabricClient.ServiceManager.RegisterServiceNotificationFilterAsync(filterDescription).GetAwaiter().GetResult();
});
await tx.CommitAsync();
}
}
我从同一个无状态服务调用它,使用远程处理基于分区哈希重新路由
public async Task RegisterGatewayServiceAsync(GatewayServiceRegistrationData data)
{
....
await GatewayManagementServiceClient.GetProxy<IServiceNotificationService>(this.Context.ServiceName.AbsoluteUri, data.ServiceName.AbsoluteUri)
.RegisterServiceNotification(data.ServiceName.AbsoluteUri);
....
}
我的GetProxy如下所示:
public static T GetProxy<T>(string service,string partition) where T : IService => CreateProxyFactoryFabricTransport().CreateServiceProxy<T>(new Uri(service), partition.ToPartitionHashFunction());
我这样做,因为我认为我应该只打电话
RegisterServiceNotificationFilterAsync
每个服务一次,我需要通知。错误/正确?
因为我希望statefull服务中的每个分区都侦听这些事件,所以我将为每个statefull服务调用OnNotification一次
public sealed class GatewayManagementService : StatefulService
{
private bool notificationRegistered = false;
public async Task RegisterGatewayServiceAsync(GatewayServiceRegistrationData data)
{
...
if(!notificationRegistered)
{
fabricClient.ServiceManager.ServiceNotificationFilterMatched += OnNotification;
notificationRegistered = true;
logger.LogInformation("ServiceNotificationEvent registered for on {node}",Context.NodeContext.NodeName);
}
....
}
}
OnNotification
服务更改时的事件。
我应该更频繁地调用RegisterServiceNotificationFilterAsync吗?我的上述假设是否有误,我能做些什么不同的事情?我还没有找到帮助我理解这一点的文件。