Resolve warnings and use features from .NET 6.

This commit is contained in:
Pete Chown
2022-09-14 18:05:16 +01:00
parent 57eb8e3727
commit 627b048144
2 changed files with 67 additions and 73 deletions

View File

@@ -1,22 +1,16 @@
using System; using System.Net.WebSockets;
using System.Net.WebSockets;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Hosting.Server;
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;
@@ -29,12 +23,12 @@ namespace Kestrel
class Application : IHttpApplication<Context> class Application : IHttpApplication<Context>
{ {
private WebSocketMiddleware wsMiddleware; private readonly WebSocketMiddleware wsMiddleware;
public Application(ILoggerFactory loggerFactory) 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); loggerFactory);
} }
@@ -43,7 +37,7 @@ namespace Kestrel
return new Context(contextFeatures); return new Context(contextFeatures);
} }
public void DisposeContext(Context context, Exception exception) public void DisposeContext(Context context, Exception? exception)
{ {
} }
@@ -53,7 +47,7 @@ namespace Kestrel
await wsMiddleware.Invoke(httpContext); await wsMiddleware.Invoke(httpContext);
} }
private async Task continueRequest(HttpContext httpContext) private async Task ContinueRequest(HttpContext httpContext)
{ {
if (httpContext.WebSockets.IsWebSocketRequest) if (httpContext.WebSockets.IsWebSocketRequest)
{ {
@@ -67,14 +61,14 @@ namespace Kestrel
else else
{ {
httpContext.Response.Headers.Add("Content-Type", new StringValues("text/plain")); httpContext.Response.Headers.Add("Content-Type", new StringValues("text/plain"));
await httpContext.Response.Body.WriteAsync(Encoding.ASCII.GetBytes("hello world")); await httpContext.Response.Body.WriteAsync(Encoding.ASCII.GetBytes("hello world\n"));
} }
} }
} }
class Program class Program
{ {
static async Task Main(string[] args) static async Task Main()
{ {
var serverOptions = new KestrelServerOptions(); var serverOptions = new KestrelServerOptions();
serverOptions.ListenAnyIP(8080); serverOptions.ListenAnyIP(8080);
@@ -85,12 +79,10 @@ namespace Kestrel
var transportFactory = new SocketTransportFactory( var transportFactory = new SocketTransportFactory(
new OptionsWrapper<SocketTransportOptions>(transportOptions), loggerFactory); new OptionsWrapper<SocketTransportOptions>(transportOptions), loggerFactory);
using (var server = new KestrelServer(new OptionsWrapper<KestrelServerOptions>(serverOptions), using var server = new KestrelServer(
transportFactory, loggerFactory)) new OptionsWrapper<KestrelServerOptions>(serverOptions), transportFactory, loggerFactory);
{
await server.StartAsync(new Application(loggerFactory), CancellationToken.None); await server.StartAsync(new Application(loggerFactory), CancellationToken.None);
Console.ReadLine(); Console.ReadLine();
} }
} }
}
}

View File

@@ -2,6 +2,8 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
</Project> </Project>