diff --git a/Net.Sdk.Web.Extensions/Net.Sdk.Web.Extensions.csproj b/Net.Sdk.Web.Extensions/Net.Sdk.Web.Extensions.csproj index d7decf2..0e50418 100644 --- a/Net.Sdk.Web.Extensions/Net.Sdk.Web.Extensions.csproj +++ b/Net.Sdk.Web.Extensions/Net.Sdk.Web.Extensions.csproj @@ -6,7 +6,7 @@ enable Library true - 0.8.8 + 0.8.9 Alexandru Macocian LICENSE true diff --git a/Net.Sdk.Web.Extensions/WebApplicationExtensions.cs b/Net.Sdk.Web.Extensions/WebApplicationExtensions.cs index 3d3aa82..c7fcbb5 100644 --- a/Net.Sdk.Web.Extensions/WebApplicationExtensions.cs +++ b/Net.Sdk.Web.Extensions/WebApplicationExtensions.cs @@ -34,52 +34,57 @@ public static class WebApplicationExtensions where TWebSocketRoute : WebSocketRouteBase { app.ThrowIfNull(); - return app.MapGet(route, static async (HttpContext context, ILogger logger, TWebSocketRoute route) => - { - if (context.WebSockets.IsWebSocketRequest) - { - var routeFilters = GetRouteFilters(context).ToList(); + return app.MapGet(route, HandleWebSocketRoute); + } - var actionContext = new ActionContext( - context, - new RouteData(), - new ActionDescriptor()); - var actionExecutingContext = new ActionExecutingContext( - actionContext, - routeFilters, - new Dictionary(), - route); - var actionExecutedContext = new ActionExecutedContext( - actionContext, - routeFilters, - route); - try - { - var processingTask = new Func(() => ProcessWebSocketRequest(route, context)); - await BeginProcessingPipeline(actionExecutingContext, actionExecutedContext, processingTask); - } - catch(WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) - { - logger.LogInformation("Websocket closed prematurely. Marking as closed"); - } - catch(OperationCanceledException) - { - logger.LogInformation("Websocket closed prematurely. Marking as closed"); - } - catch(Exception ex) - { - logger.LogError(ex, "Encountered exception while handling websocket. Closing"); - } - finally - { - await route.SocketClosed(); - } - } - else - { - context.Response.StatusCode = StatusCodes.Status400BadRequest; - } - }); + private static async Task HandleWebSocketRoute(HttpContext context, TWebSocketRoute route, ILogger logger) + where TWebSocketRoute : WebSocketRouteBase + { + if (!context.WebSockets.IsWebSocketRequest) + { + logger.LogError("WebSocket request expected"); + return Results.BadRequest("WebSocket request expected"); + } + + var routeFilters = GetRouteFilters(context).ToList(); + var actionContext = new ActionContext( + context, + new RouteData(), + new ActionDescriptor()); + var actionExecutingContext = new ActionExecutingContext( + actionContext, + routeFilters, + new Dictionary(), + route); + var actionExecutedContext = new ActionExecutedContext( + actionContext, + routeFilters, + route); + try + { + var processingTask = new Func(() => ProcessWebSocketRequest(route, context)); + await BeginProcessingPipeline(actionExecutingContext, actionExecutedContext, processingTask); + return Results.Empty; + } + catch (WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) + { + logger.LogInformation("Websocket closed prematurely. Marking as closed"); + return Results.Empty; + } + catch (OperationCanceledException) + { + logger.LogInformation("Websocket closed prematurely. Marking as closed"); + return Results.Empty; + } + catch (Exception ex) + { + logger.LogError(ex, "Encountered exception while handling websocket. Closing"); + return Results.Empty; + } + finally + { + await route.SocketClosed(); + } } private static async Task ProcessWebSocketRequest(WebSocketRouteBase route, HttpContext httpContext)