带有 OWIN 自托管侦听器的 Azure Service Fabric

Azure Service Fabric with OWIN self-host listener

在我的一项无状态服务中,我想包括两个侦听器:

当我尝试以标准方式注册创建的侦听器时,我遇到一个问题,即基于 OWIN 的侦听器在初始化后自动关闭:

负责注册服务的部分:

        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 端口设置:

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

如何解决这个问题并注册这两个监听器?

当你有多个听众时,你必须给他们唯一的名字。您的服务可能因此而失败,这将导致它一遍又一遍地打开和关闭:

    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")
        }
    }