同一服务上的服务结构多个 WCF 终结点

Service fabric multiple WCF endpoints on same service

我正在尝试为服务创建 2 个端点:net.tcp 和 http。两个端点将具有相同的端点名称。 如何在 Service fabric 中实现?

在SF中没有运行的时候在App config中很容易定义,这样:

<service behaviorConfiguration="DefaultBehavior" name="ContractImplementation">
        <endpoint address="net.tcp://localhost:6000/ContractName" binding="netTcpBinding" bindingConfiguration="netTcpBinding" contract="ContractName" />
        <endpoint address="http://localhost:6001/ContractName" binding="basicHttpBinding" bindingConfiguration="httpBinding" contract="ContractName" />
</service>

在SF中运行时,我通过创建WcfCommunictionListener对象来创建监听器。我无法创建另一个具有不同绑定的,因为它抱怨端点名称已在使用中。

如文档中所述here:

When creating multiple listeners for a service, each listener must be given a unique name.

端点名称必须有不同的名称,您可以在创建侦听器的逻辑中处理加载,您应该为每个端点创建一个侦听器并传递每个侦听器的名称;

像这样:

protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
{
    return new[]
    {
        new ServiceReplicaListener(context =>
          new WcfCommunicationListener<ICalculator>(
              wcfServiceObject:this,
              serviceContext:context,
              endpointResourceName: "WcfServiceEndpoint1",
              listenerBinding: WcfUtility.CreateTcpListenerBinding()
          )
        ),
        new ServiceReplicaListener(context =>
          new WcfCommunicationListener<ICalculator>(
              wcfServiceObject:this,
              serviceContext:context,
              endpointResourceName: "WcfServiceEndpoint2",
              listenerBinding: WcfUtility.CreateTcpListenerBinding()
          )
       )
   };
}

有关如何使用 WcfCommunicationListener 的更多信息,请查看 here