Service-Fabric绑定到多个端点

时间:2017-02-06 16:34:51

标签: c# asp.net-core azure-service-fabric

是否可以绑定服务结构应用程序以侦听多个端口?

基本上我正在尝试使用面向http:80和https:443的公共服务,并将任何http请求重定向到https。

我创建了一个新的ASP.net核心服务,它可以单独运行。即使用SSL 443或仅使用非SSL 80,但当我添加ServiceInstanceListeners时,它只会失败!

Service Fabric Explorer在多次超时后发出以下错误:

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()

这很奇怪,因为两个听众都有不同的名字 - 所以看起来如此。在那里我应该设置我错过的听众名称吗?

我正在使用Asp.net Core模板。我的无状态服务代码如下:

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

2 个答案:

答案 0 :(得分:10)

我需要在ServiceInstanceListener上设置具有构造函数

的名称
public ServiceInstanceListener(Func<StatelessServiceContext, ICommunicationListener> createCommunicationListener, string name = "");

我没有意识到它有额外的参数:)

答案 1 :(得分:1)

您可以使用以下代码自动执行所有操作,并在需要的地方记录所需信息。

var currentEndpoint = "";
try
{
  IList<ServiceInstanceListener> listeners = new List<ServiceInstanceListener>();
  var endpoints = FabricRuntime.GetActivationContext().GetEndpoints();

  foreach (var endpoint in endpoints)
  {
    currentEndpoint = endpoint.Name;
    logger.LogInformation("Website trying to LISTEN : " + currentEndpoint);

    var webListner = new ServiceInstanceListener(serviceContext =>
       new WebListenerCommunicationListener(serviceContext, endpoint.Name, (url, listener) =>
       {
         url = endpoint.Protocol + "://+:" + endpoint.Port;
         logger.LogInformation("Website Listening : " + currentEndpoint);
         return new WebHostBuilder().UseWebListener()       .UseContentRoot(Directory.GetCurrentDirectory())
              .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
              .UseStartup<Startup>()
              .UseUrls(url)
              .Build();
       }), endpoint.Name.ToString());
    listeners.Add(webListner);
  }
  return listeners;
}
catch (Exception ex)
{
  logger.LogError("Exception occured while listening endpoint: " + currentEndpoint, ex);
  throw;
}