Search code examples
azure-service-fabric

Unable to use multiple ServiceInstanceListener objects in CreateServiceInstanceListeners


I have created a Service Fabric application and the StatelessService.CreateServiceInstanceListeners returns multiple ServiceInstanceListener objects. The service listeners are opened, but aborted almost immediately. It then opens the listeners again (without going through CreateServiceInstanceListeners, aborts them, ...

When I use only one of the service listeners, then everything works fine.


Solution

  • The returned service instance listeners are added to a ServiceListenerInstanceCollection ([code][1]) and this fails if there is already a service listener instance with the same name in the collection.

    The ServiceInstanceListener constructor has the following implementation:

    public ServiceInstanceListener(
       Func<StatelessServiceContext, ICommunicationListener> createCommunicationListener,
       string name = "")
    {
      this.CreateCommunicationListener = createCommunicationListener;
      this.Name = name;
    }
    

    The default name is an empty string, so if you don't specify a name, then the returned enumeration contains multiple listeners with the name "" and this raises an exception. The default implementation aborts the already opened listeners and restarts them.

    The solution is simple. Simply specify a (unique) name when creating the ServiceInstanceListener.