Support https (resolves #3).
This commit is contained in:
34
Program.cs
34
Program.cs
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user