Minimal changes for .NET 6.0.

This commit is contained in:
Pete Chown
2022-09-14 17:53:28 +01:00
parent b7c59a94ab
commit 57eb8e3727
2 changed files with 99 additions and 130 deletions

View File

@@ -4,13 +4,13 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets; using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
using Microsoft.AspNetCore.WebSockets; using Microsoft.AspNetCore.WebSockets;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
@@ -31,10 +31,11 @@ namespace Kestrel
{ {
private WebSocketMiddleware wsMiddleware; private WebSocketMiddleware wsMiddleware;
public Application() public Application(ILoggerFactory loggerFactory)
{ {
var wsOptions = new WebSocketOptions(); var wsOptions = new WebSocketOptions();
wsMiddleware = new WebSocketMiddleware(continueRequest, new OptionsWrapper<WebSocketOptions>(wsOptions)); wsMiddleware = new WebSocketMiddleware(continueRequest, new OptionsWrapper<WebSocketOptions>(wsOptions),
loggerFactory);
} }
public Context CreateContext(IFeatureCollection contextFeatures) public Context CreateContext(IFeatureCollection contextFeatures)
@@ -71,28 +72,6 @@ namespace Kestrel
} }
} }
class ApplicationLifetime : IApplicationLifetime
{
private readonly CancellationTokenSource startedSource = new CancellationTokenSource();
private readonly CancellationTokenSource stoppingSource = new CancellationTokenSource();
private readonly CancellationTokenSource stoppedSource = new CancellationTokenSource();
public CancellationToken ApplicationStarted => startedSource.Token;
public CancellationToken ApplicationStopping => stoppingSource.Token;
public CancellationToken ApplicationStopped => stoppedSource.Token;
public void StopApplication()
{
lock (stoppingSource)
{
if (!stoppingSource.Token.IsCancellationRequested)
stoppingSource.Cancel(throwOnFirstException: false);
}
}
}
class Program class Program
{ {
static async Task Main(string[] args) static async Task Main(string[] args)
@@ -102,15 +81,14 @@ namespace Kestrel
var transportOptions = new SocketTransportOptions(); var transportOptions = new SocketTransportOptions();
var loggerFactory = new NullLoggerFactory(); var loggerFactory = new NullLoggerFactory();
var applicationLifetime = new ApplicationLifetime();
var transportFactory = new SocketTransportFactory( var transportFactory = new SocketTransportFactory(
new OptionsWrapper<SocketTransportOptions>(transportOptions), applicationLifetime, loggerFactory); new OptionsWrapper<SocketTransportOptions>(transportOptions), loggerFactory);
using (var server = new KestrelServer(new OptionsWrapper<KestrelServerOptions>(serverOptions), using (var server = new KestrelServer(new OptionsWrapper<KestrelServerOptions>(serverOptions),
transportFactory, loggerFactory)) transportFactory, loggerFactory))
{ {
await server.StartAsync(new Application(), CancellationToken.None); await server.StartAsync(new Application(loggerFactory), CancellationToken.None);
Console.ReadLine(); Console.ReadLine();
} }
} }

View File

@@ -1,16 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>7.1</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Core" Version="2.1.7" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.1.7" />
</ItemGroup>
</Project> </Project>