Minimal changes for .NET 6.0.
This commit is contained in:
214
Program.cs
214
Program.cs
@@ -1,118 +1,96 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
using System.Text;
|
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;
|
||||||
|
|
||||||
namespace Kestrel
|
namespace Kestrel
|
||||||
{
|
{
|
||||||
class Context
|
class Context
|
||||||
{
|
{
|
||||||
public IFeatureCollection features;
|
public IFeatureCollection features;
|
||||||
|
|
||||||
public Context(IFeatureCollection features)
|
public Context(IFeatureCollection features)
|
||||||
{
|
{
|
||||||
this.features = features;
|
this.features = features;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Application : IHttpApplication<Context>
|
class Application : IHttpApplication<Context>
|
||||||
{
|
{
|
||||||
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)
|
||||||
return new Context(contextFeatures);
|
{
|
||||||
}
|
return new Context(contextFeatures);
|
||||||
|
}
|
||||||
public void DisposeContext(Context context, Exception exception)
|
|
||||||
{
|
public void DisposeContext(Context context, Exception exception)
|
||||||
}
|
{
|
||||||
|
}
|
||||||
public async Task ProcessRequestAsync(Context context)
|
|
||||||
{
|
public async Task ProcessRequestAsync(Context context)
|
||||||
HttpContext httpContext = new DefaultHttpContext(context.features);
|
{
|
||||||
await wsMiddleware.Invoke(httpContext);
|
HttpContext httpContext = new DefaultHttpContext(context.features);
|
||||||
}
|
await wsMiddleware.Invoke(httpContext);
|
||||||
|
}
|
||||||
private async Task continueRequest(HttpContext httpContext)
|
|
||||||
{
|
private async Task continueRequest(HttpContext httpContext)
|
||||||
if (httpContext.WebSockets.IsWebSocketRequest)
|
{
|
||||||
{
|
if (httpContext.WebSockets.IsWebSocketRequest)
|
||||||
var socket = await httpContext.WebSockets.AcceptWebSocketAsync();
|
{
|
||||||
var message = Encoding.ASCII.GetBytes("hello world");
|
var socket = await httpContext.WebSockets.AcceptWebSocketAsync();
|
||||||
await socket.SendAsync(new ArraySegment<byte>(message), WebSocketMessageType.Text, true,
|
var message = Encoding.ASCII.GetBytes("hello world");
|
||||||
CancellationToken.None);
|
await socket.SendAsync(new ArraySegment<byte>(message), WebSocketMessageType.Text, true,
|
||||||
|
CancellationToken.None);
|
||||||
await socket.ReceiveAsync(new byte[4096], CancellationToken.None);
|
|
||||||
}
|
await socket.ReceiveAsync(new byte[4096], CancellationToken.None);
|
||||||
else
|
}
|
||||||
{
|
else
|
||||||
httpContext.Response.Headers.Add("Content-Type", new StringValues("text/plain"));
|
{
|
||||||
await httpContext.Response.Body.WriteAsync(Encoding.ASCII.GetBytes("hello world"));
|
httpContext.Response.Headers.Add("Content-Type", new StringValues("text/plain"));
|
||||||
}
|
await httpContext.Response.Body.WriteAsync(Encoding.ASCII.GetBytes("hello world"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
class ApplicationLifetime : IApplicationLifetime
|
|
||||||
{
|
class Program
|
||||||
private readonly CancellationTokenSource startedSource = new CancellationTokenSource();
|
{
|
||||||
private readonly CancellationTokenSource stoppingSource = new CancellationTokenSource();
|
static async Task Main(string[] args)
|
||||||
private readonly CancellationTokenSource stoppedSource = new CancellationTokenSource();
|
{
|
||||||
|
var serverOptions = new KestrelServerOptions();
|
||||||
public CancellationToken ApplicationStarted => startedSource.Token;
|
serverOptions.ListenAnyIP(8080);
|
||||||
|
|
||||||
public CancellationToken ApplicationStopping => stoppingSource.Token;
|
var transportOptions = new SocketTransportOptions();
|
||||||
|
var loggerFactory = new NullLoggerFactory();
|
||||||
public CancellationToken ApplicationStopped => stoppedSource.Token;
|
|
||||||
|
var transportFactory = new SocketTransportFactory(
|
||||||
public void StopApplication()
|
new OptionsWrapper<SocketTransportOptions>(transportOptions), loggerFactory);
|
||||||
{
|
|
||||||
lock (stoppingSource)
|
using (var server = new KestrelServer(new OptionsWrapper<KestrelServerOptions>(serverOptions),
|
||||||
{
|
transportFactory, loggerFactory))
|
||||||
if (!stoppingSource.Token.IsCancellationRequested)
|
{
|
||||||
stoppingSource.Cancel(throwOnFirstException: false);
|
await server.StartAsync(new Application(loggerFactory), CancellationToken.None);
|
||||||
}
|
Console.ReadLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
class Program
|
}
|
||||||
{
|
|
||||||
static async Task Main(string[] args)
|
|
||||||
{
|
|
||||||
var serverOptions = new KestrelServerOptions();
|
|
||||||
serverOptions.ListenAnyIP(8080);
|
|
||||||
|
|
||||||
var transportOptions = new SocketTransportOptions();
|
|
||||||
var loggerFactory = new NullLoggerFactory();
|
|
||||||
var applicationLifetime = new ApplicationLifetime();
|
|
||||||
|
|
||||||
var transportFactory = new SocketTransportFactory(
|
|
||||||
new OptionsWrapper<SocketTransportOptions>(transportOptions), applicationLifetime, loggerFactory);
|
|
||||||
|
|
||||||
using (var server = new KestrelServer(new OptionsWrapper<KestrelServerOptions>(serverOptions),
|
|
||||||
transportFactory, loggerFactory))
|
|
||||||
{
|
|
||||||
await server.StartAsync(new Application(), CancellationToken.None);
|
|
||||||
Console.ReadLine();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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>
|
</Project>
|
||||||
<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>
|
|
||||||
Reference in New Issue
Block a user