问题描述
是否可以将Service Fabric应用程序绑定为在多个端口上侦听?
Is it possible to bind a service fabric app to listen on multiple ports?
基本上,我正在尝试提供面向公众的服务,该服务侦听http:80和https:443,并将所有http请求重定向到https.
Basically I'm trying to have a public facing service which listens on http:80 and https:443, and redirects any http requests to https.
我创建了一个新的ASP.net Core服务,它可以单独正常运行.IE.使用SSL 443或仅使用非SSL 80,但是当我同时添加两个 ServiceInstanceListeners
时,它只会失败!
I created a new ASP.net Core service, it works fine individually. I.e. with SSL 443 or just non-SSL 80, but when I add both ServiceInstanceListeners
it just fails!
Service Fabric Explorer超时几次后会显示以下错误:
Service Fabric Explorer says the following error after timing out several times:
Unhealthy event: SourceId='System.RA', Property='ReplicaOpenStatus', HealthState='Warning', ConsiderWarningAsError=false.
Replica had multiple failures in API call: IStatelessServiceInstance.Open(); Error = System.Fabric.FabricElementAlreadyExistsException (-2146233088)
Unique Name must be specified for each listener when multiple communication listeners are used
at Microsoft.ServiceFabric.Services.Communication.ServiceEndpointCollection.AddEndpointCallerHoldsLock(String listenerName, String endpointAddress)
at Microsoft.ServiceFabric.Services.Communication.ServiceEndpointCollection.AddEndpoint(String listenerName, String endpointAddress)
at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.d__0.MoveNext()
这很奇怪,因为两个侦听器都有不同的名称-如此看来.我应该在某个地方设置我错过的收听者姓名吗?
Which is strange because both listeners have different names -- so it would seem. IS there somewhere I should be setting the listener name that I have missed?
我正在为此使用Asp.net Core模板.我的无状态服务代码如下:
I'm using the Asp.net Core template for this. My Stateless Service code is as follows:
internal sealed class Web : StatelessService
{
public Web(StatelessServiceContext context)
: base(context)
{ }
/// <summary>
/// Optional override to create listeners (like tcp, http) for this service instance.
/// </summary>
/// <returns>The collection of listeners.</returns>
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new ServiceInstanceListener[]
{
new ServiceInstanceListener(serviceContext =>
new WebListenerCommunicationListener(serviceContext, "ServiceEndpointHttps", url =>
{
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");
return new WebHostBuilder()
.UseWebListener()
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls(url)
.Build();
})),
new ServiceInstanceListener(serviceContext =>
new WebListenerCommunicationListener(serviceContext, "ServiceEndpointHttp", url =>
{
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");
return new WebHostBuilder()
.UseWebListener()
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls(url)
.Build();
}))
};
}
}
推荐答案
我需要在具有构造函数的 ServiceInstanceListener
上设置名称
I needed to set the name on ServiceInstanceListener
which has the constructor
public ServiceInstanceListener(Func<StatelessServiceContext, ICommunicationListener> createCommunicationListener, string name = "");
我没有意识到它还有多余的参数:)
I didn't realize it had extra params :)
这篇关于Service-Fabric绑定到多个端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!