I have a Service Fabric Stateless Asp.Net Core service.
The service works with an HTTP endpoint. I need to support an HTTPS endpoint along with the HTTP
My steps so far:
- Added the certificate to Azure KeyVault
Updated ApplicationManifest.xml with:
<Policies> <EndpointBindingPolicy EndpointRef="ServiceEndpointHttps" CertificateRef="Certificate" /> </Policies>
And at the end
<Certificates>
<EndpointCertificate X509StoreName="MY" X509FindValue="XXXX" Name="Certificate" />
</Certificates>
- Added the Endpoint in ServiceManifest.xml with port 443
I'm now left with enabling the HTTPS endpoint. In Program.cs I have this:
public static void Main(string[] args)
{
ServiceRuntime.RegisterServiceAsync("MyType", context => new WebHostingService(context, "ServiceEndpoint")).GetAwaiter().GetResult();
Thread.Sleep(Timeout.Infinite);
}
internal sealed class WebHostingService : StatelessService, ICommunicationListener
{
private readonly string _endpointName;
private IWebHost _webHost;
public WebHostingService(StatelessServiceContext serviceContext, string endpointName)
: base(serviceContext)
{
_endpointName = endpointName;
}
#region StatelessService
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new[] { new ServiceInstanceListener(_ => this) };
}
#endregion StatelessService
#region ICommunicationListener
void ICommunicationListener.Abort()
{
_webHost?.Dispose();
}
Task ICommunicationListener.CloseAsync(CancellationToken cancellationToken)
{
_webHost?.Dispose();
return Task.FromResult(true);
}
Task<string> ICommunicationListener.OpenAsync(CancellationToken cancellationToken)
{
var endpoint = FabricRuntime.GetActivationContext().GetEndpoint(_endpointName);
string serverUrl = $"{endpoint.Protocol}://{FabricRuntime.GetNodeContext().IPAddressOrFQDN}:{endpoint.Port}";
_webHost = new WebHostBuilder().UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls(serverUrl)
.Build();
_webHost.Start();
return Task.FromResult(serverUrl);
}
#endregion ICommunicationListener
}
How can I register the HTTPS endpoint here?
Adding a second ServiceRuntime.RegisterServiceAsync doesn't work.
Also, After enabling it, how I do install (from the portal or powershell) the certificates from KeyVault to the Virtual Machines Scale sets that are already deployed and running?