【问题标题】:Setting up https endpoint in service fabric for ASP .Net Core 2在服务结构中为 ASP .Net Core 2 设置 https 端点
【发布时间】:2018-03-05 20:48:26
【问题描述】:

如何在 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 做些什么吗?

【问题讨论】:

    标签: azure-service-fabric asp.net-core-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()
                  ));
            }
    

    【讨论】:

      【解决方案2】:

      要运行 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

      【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 2018-06-19
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多