kumar
kumar

Reputation: 9387

Setting up https endpoint in service fabric for ASP .Net Core 2

How do I setup up https endpoint in service fabric for ASP .Net Core 2 running on windows 2016 server. So for I have added the endpoint in ServiceManifest.xml

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

and also in ApplicationManifest.xml I added

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

When I deploy it does not create the endpoint for https. Is there anything else I need to do for https?

Upvotes: 0

Views: 1609

Answers (2)

kumar
kumar

Reputation: 9387

Add Nuget package

Microsoft.ServiceFabric.AspNetCore.HttpSys in

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

for multiple endpoint for http and 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()
              ));
        }

Upvotes: 1

LoekD
LoekD

Reputation: 11470

To run WebListener, try this code in your service:

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

To run Kestrel, try using this code, in Startup.cs:

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

More info here.

Upvotes: 0

Related Questions