订阅Service Fabric集群级别事件

时间:2017-07-21 21:53:43

标签: azure-service-fabric service-fabric-stateless

我正在尝试创建一个服务,该服务将为我的服务结构集群中运行的应用程序更新服务端点的外部列表。 (基本上我需要在我的内部F5负载均衡器中复制Azure负载均衡器。)

在上个月的Service Fabric Q& A期间,团队向我指出RegisterServiceNotificationFilterAsync

我使用此方法创建了无状态服务,并将其部署到我的开发集群。然后我通过运行ASP.NET Core Stateless服务模板创建了一个新服务。

我预计当我部署第二个服务时,断点将在我的第一个服务中出现,表示已添加服务。但是没有破坏点。

我在互联网上发现这种事情的例子很少,所以我在这里要求其他人这样做,并告诉我哪里出错了。

以下是我的服务试图捕获应用程序更改的代码:

protected override async Task RunAsync(CancellationToken cancellationToken)
{

    var fabricClient = new FabricClient();

    long? filterId = null;


    try
    {
        var filterDescription = new ServiceNotificationFilterDescription
        {
            Name = new Uri("fabric:")
        };
        fabricClient.ServiceManager.ServiceNotificationFilterMatched += ServiceManager_ServiceNotificationFilterMatched;
        filterId = await fabricClient.ServiceManager.RegisterServiceNotificationFilterAsync(filterDescription);


        long iterations = 0;

        while (true)
        {
            cancellationToken.ThrowIfCancellationRequested();

            ServiceEventSource.Current.ServiceMessage(this.Context, "Working-{0}", ++iterations);

            await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
        }
    }
    finally
    {
        if (filterId != null)
            await fabricClient.ServiceManager.UnregisterServiceNotificationFilterAsync(filterId.Value);
    }



}

private void ServiceManager_ServiceNotificationFilterMatched(object sender, EventArgs e)
{
    Debug.WriteLine("Change Occured");
}

如果你有任何关于如何实现这一目标的提示,我很乐意看到它们。

1 个答案:

答案 0 :(得分:1)

您需要将MatchNamePrefix设置为true,如下所示:

    var filterDescription = new ServiceNotificationFilterDescription
    {
        Name = new Uri("fabric:"),
        MatchNamePrefix = true
    };

否则它只会匹配特定服务。在我的应用程序中,当此参数设置为true时,我可以捕获群集范围的事件。