在ASP .Net Core 2的服务结构中设置https端点

时间:2018-02-27 14:41:41

标签: azure-service-fabric asp.net-core-2.0

如何在 Windows 2016服务器上运行的ASP .Net Core 2 的服务结构中设置https端点。因此,我在ServiceManifest.xml中添加了端点

<Endpoint Protocol="https" Name="ServiceEndpointHttps" Type="Input" Port="8373" />

并且在ApplicationManifest.xml中我添加了

  <EndpointBindingPolicy EndpointRef="ServiceEndpointHttps" CertificateRef="my_api_cert" />
  <Certificates>
    <EndpointCertificate X509FindValue="certthumbprint" Name="my_api_cert" />
  </Certificates>

部署时,不会为https创建端点。我还需要为https做些什么吗?

2 个答案:

答案 0 :(得分:1)

添加Nuget包

  

Microsoft.ServiceFabric.AspNetCore.HttpSys   在

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners() {
  return new ServiceInstanceListener[] 
  {
    new ServiceInstanceListener(serviceContext => 
      new HttpSysCommunicationListener(serviceContext, "ServiceEndpointHttps", (url, listener) => 
        new WebHostBuilder()
          .UseHttpSys()
          .ConfigureService(
            service => service
              .AddSingleton<StatelessServiceContext>(serviceContext))
          .UseContentRoot(Directory.GetCurrentDirectory())
          .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
          .UseStartup<Startup>()
          .UseUrls(url)
          .Build()))
  };
}

用于http和https的多个端点

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
        {
            var endpoints = Context.CodePackageActivationContext.GetEndpoints().Where(endpoint => endpoint.Protocol == EndpointProtocol.Http || endpoint.Protocol == EndpointProtocol.Https);

            return endpoints.Select(endpoint => new ServiceInstanceListener(serviceContext =>
              // New Service Fabric listener for each endpoint configuration in the manifest.
              new HttpSysCommunicationListener(serviceContext, endpoint.Name, (url, listener) =>
              {
                  return new WebHostBuilder()
                                    .UseHttpSys()
                                    .ConfigureServices(
                                        services => services
                                            .AddSingleton<StatelessServiceContext>(serviceContext))
                                    .UseContentRoot(Directory.GetCurrentDirectory())
                                    .UseStartup<Startup>()
                                    .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                                    .UseUrls(url)
                                    .Build();
              }), endpoint.Name.ToString()
              ));
        }

答案 1 :(得分:0)

要运行WebListener,请在您的服务中尝试以下代码:

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
    return new ServiceInstanceListener[]
    {
        new ServiceInstanceListener(serviceContext =>
            new WebListenerCommunicationListener(serviceContext, "ServiceEndpoint", 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();
            }))
    };
}

要运行Kestrel,请尝试在Startup.cs中使用此代码:

public void ConfigureServices(IServiceCollection services)
{
    X509Certificate2 myCert = GetMyCertificate();
    services.Configure<KestrelServerOptions>(opt =>
    {
        opt.UseHttps(myCert);
    });
}

更多信息here