1
votes

In one of my stateless service I would like to include two listeners:

When I try to register created listeners in standard manner I have a problem that listener based on OWIN is automaticaly closed after initialization:

Diagonstic events

Part responsible for registering services:

        protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
        {
           return new List<ServiceReplicaListener>
           {
              new ServiceReplicaListener(parameters => new OwinCommunicationListener("account",new Startup(), parameters)),
              new ServiceReplicaListener(parameters => new ServiceRemotingListener<AccountService>(parameters, this))

        };
    }

ServiceManifest ports setup:

<Resources>
  <Endpoints>
    <Endpoint Name="ServiceEndpoint" />
    <Endpoint Name="WebApiEndpoint" Type="Input" Protocol="http" Port="8083"/>
    <Endpoint Name="ReplicatorEndpoint" />
  </Endpoints>
</Resources>

OwinCommunicationListener:

public class OwinCommunicationListener: ICommunicationListener
{
    private readonly IOwinAppBuilder startup;
    private readonly string appRoot;
    private IDisposable serverHandle;
    private string listeningAddress;
    private readonly ServiceInitializationParameters serviceInitializationParameters;

    public OwinCommunicationListener(string appRoot, IOwinAppBuilder startup, ServiceInitializationParameters serviceInitializationParameters)
    {
        this.startup = startup;
        this.appRoot = appRoot;
        this.serviceInitializationParameters = serviceInitializationParameters;
    }

    public Task<string> OpenAsync(CancellationToken cancellationToken)
    {
        EndpointResourceDescription serviceEndpoint = serviceInitializationParameters.CodePackageActivationContext.GetEndpoint("WebApiEndpointB");
        int port = serviceEndpoint.Port;
        listeningAddress = string.Format(CultureInfo.InvariantCulture,"http://+:{0}/{1}",port,string.IsNullOrWhiteSpace(appRoot)? string.Empty: appRoot.TrimEnd('/') + '/');
        serverHandle = WebApp.Start(listeningAddress, appBuilder => startup.Configuration(appBuilder));
        string publishAddress = listeningAddress.Replace("+", FabricRuntime.GetNodeContext().IPAddressOrFQDN);
        ServiceEventSource.Current.Message("Listening on {0}", publishAddress);
        return Task.FromResult(publishAddress);
    }
    ...
}

How can I sove this problem and have these two listeners registered?

1

1 Answers

2
votes

When you have more than one listener, you have to give them unique names. Your service may be failing because of this, which would cause it to open and close over and over:

    protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
    {
        return new List<ServiceReplicaListener>
       {
          new ServiceReplicaListener(parameters => new OwinCommunicationListener("account",new Startup(), parameters), "accountListener"),
          new ServiceReplicaListener(parameters => new ServiceRemotingListener<AccountService>(parameters, this), "remotingListener")
        }
    }