Support https (resolves #3).

This commit is contained in:
Pete Chown
2023-01-07 18:59:00 +00:00
parent 89d4f3303f
commit c14d75b930
3 changed files with 55 additions and 2 deletions

View File

@@ -1,8 +1,10 @@
using System.Net.WebSockets;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Https;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
using Microsoft.AspNetCore.WebSockets;
using Microsoft.Extensions.Logging.Abstractions;
@@ -66,12 +68,39 @@ class Application : IHttpApplication<Context>
}
}
class AppServices : IServiceProvider
{
public object? GetService(Type serviceType)
{
if (serviceType == typeof(ILoggerFactory))
return new NullLoggerFactory();
return null;
}
}
class Program
{
static async Task Main()
{
var serverOptions = new KestrelServerOptions();
serverOptions.ListenAnyIP(8080);
KestrelServerOptions serverOptions = new();
if (File.Exists("localhost.pfx"))
{
HttpsConnectionAdapterOptions httpsOptions = new()
{
ServerCertificate = new X509Certificate2("localhost.pfx")
};
serverOptions.ListenAnyIP(8080, options =>
{
options.KestrelServerOptions.ApplicationServices = new AppServices();
options.UseHttps(httpsOptions);
});
}
else
{
serverOptions.ListenAnyIP(8080);
}
var transportOptions = new SocketTransportOptions();
var loggerFactory = new NullLoggerFactory();
@@ -83,6 +112,7 @@ class Program
new OptionsWrapper<KestrelServerOptions>(serverOptions), transportFactory, loggerFactory);
await server.StartAsync(new Application(loggerFactory), CancellationToken.None);
Console.WriteLine("Server started");
await Task.Delay(Timeout.Infinite);
}
}