1

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

当我尝试以标准方式注册创建的侦听器时,我遇到了一个问题,即基于 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);
    }
    ...
}

我怎样才能解决这个问题并让这两个听众注册?

4

1 回答 1

2

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

    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")
        }
    }
于 2016-03-26T00:39:07.077 回答