在我的一项无状态服务中,我想包括两个听众:
- Service Fabric 提供的监听器,用于服务之间的内部通信
- 根据示例实现的基于OWIN Self-Host的监听器(https://github.com/Azure/azure-content/blob/master/articles/service-fabric/service-fabric-reliable-services-communication-webapi.md)
当我尝试以标准方式注册创建的侦听器时,我遇到了一个问题,即基于 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);
}
...
}
我怎样才能解决这个问题并让这两个听众注册?