Search code examples
wcfazure-service-fabric

Service fabric multiple WCF endpoints on same service


I am trying to create 2 endpoints for a service: net.tcp and http. Both endpoints will have the same endpoint name. How to do it in Service fabric ?

It is easy to define it in App config when not running in SF, this way:

<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>

When running in SF, I create a listener by creating WcfCommunictionListener object. I can't create another one with different binding, as it complains the endpoint name is already in use.


Solution

  • As described in the docs here:

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

    The endpoint names must have different names, and you can handle the loading in the logic that create the listener, you should create one listener for each endpoint and pass the name of each of then;

    Something like this:

    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()
              )
           )
       };
    }
    

    For more information on how to use WcfCommunicationListener check here