【问题标题】:Service Fabric -- CreateServiceInstanceListeners -- Multiple Listeners -- "yield return" or "return new[]"Service Fabric -- CreateServiceInstanceListeners -- 多个侦听器 -- "yield return" 或 "return new[]"
【发布时间】:2019-01-18 17:24:19
【问题描述】:

如果您的 Service Fabric 服务有多个侦听器,我注意到您可以使用 2 种不同的模式创建 CreateServiceInstanceListeners。 (.Net 框架/有状态/无状态)

第一种方法是产生返回值,而第二种方法是返回一个数组。

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
    yield return new ServiceInstanceListener(serviceContext => new OwinCommunicationListener(Startup.ConfigureApp, serviceContext, ServiceEventSource.Current, "ServiceEndpoint")),
    yield return new ServiceInstanceListener(wcfContext => new WcfCommunicationListener<IWebApi>(wcfServiceObject: this, serviceContext: wcfContext,endpointResourceName: "WcfEndpoint",listenerBinding: WcfUtility.CreateTcpListenerBinding()),"WcfEndpoint")
}

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
    return new[]
    {
        new ServiceInstanceListener(serviceContext => new OwinCommunicationListener(Startup.ConfigureApp, serviceContext, ServiceEventSource.Current, "ServiceEndpoint")),
        new ServiceInstanceListener(wcfContext => new WcfCommunicationListener<IWebApi>(wcfServiceObject: this, serviceContext: wcfContext,endpointResourceName: "WcfEndpoint",listenerBinding: WcfUtility.CreateTcpListenerBinding()),"WcfEndpoint")
    };
}

使用一个比另一个有什么优势吗?

【问题讨论】:

    标签: azure-service-fabric


    【解决方案1】:

    没有太大区别。每个服务实例对返回的枚举进行一次迭代。

    这是从GitHub 上截取的代码。

    if (this.instanceListeners == null)
    {
        this.instanceListeners = this.userServiceInstance.CreateServiceInstanceListeners();
    }
    
    var endpointsCollection = new ServiceEndpointCollection();
    var listenerOpenedCount = 0;
    
    foreach (var entry in this.instanceListeners)
    {
        var communicationListener = entry.CreateCommunicationListener(this.serviceContext);
        this.AddCommunicationListener(communicationListener);
        var endpointAddress = await communicationListener.OpenAsync(cancellationToken);
        endpointsCollection.AddEndpoint(entry.Name, endpointAddress);
        listenerOpenedCount++;
    
        var traceMsg = entry.Name.Equals(ServiceInstanceListener.DefaultName)
            ? "Opened communication listener with default name."
            : $"Opened communication listener with name {entry.Name}.";
    
        ServiceTrace.Source.WriteInfoWithId(TraceType, this.traceId, traceMsg);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-22
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 2016-01-19
      • 2019-06-19
      • 2018-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多