From 47300326c2f75e33aca08dc54697bce01b11e75f Mon Sep 17 00:00:00 2001 From: Leshchev Artem Date: Mon, 8 Jul 2024 11:06:18 +0200 Subject: [PATCH] Fix DI in ErrorsMiddlewarte (#18) Co-authored-by: Artem Leshchev --- .../ErrorsMiddleware.cs | 14 ++++++-------- src/__SolutionItems/CommonAssemblyInfo.cs | 6 +++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/Bss.Platform.Api.Middlewares/ErrorsMiddleware.cs b/src/Bss.Platform.Api.Middlewares/ErrorsMiddleware.cs index 4e48d44..3c489d0 100644 --- a/src/Bss.Platform.Api.Middlewares/ErrorsMiddleware.cs +++ b/src/Bss.Platform.Api.Middlewares/ErrorsMiddleware.cs @@ -4,14 +4,13 @@ using Bss.Platform.Api.Middlewares.Interfaces; using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Bss.Platform.Api.Middlewares; -public class ErrorsMiddleware(RequestDelegate next, ILogger logger, IServiceProvider serviceProvider) +public class ErrorsMiddleware(RequestDelegate next) { - public async Task Invoke(HttpContext context) + public async Task InvokeAsync(HttpContext context, ILogger logger, IStatusCodeResolver? statusCodeResolver) { try { @@ -21,16 +20,15 @@ public async Task Invoke(HttpContext context) { logger.LogError(e, "Request failed"); - await this.HandleExceptionAsync(context, e); + await HandleExceptionAsync(context, e, statusCodeResolver); } } - private Task HandleExceptionAsync(HttpContext context, Exception exception) + private static async Task HandleExceptionAsync(HttpContext context, Exception exception, IStatusCodeResolver? statusCodeResolver) { context.Response.ContentType = MediaTypeNames.Text.Plain; - context.Response.StatusCode = - (int)(serviceProvider.GetService()?.Resolve(exception) ?? HttpStatusCode.InternalServerError); + context.Response.StatusCode = (int)(statusCodeResolver?.Resolve(exception) ?? HttpStatusCode.InternalServerError); - return context.Response.WriteAsync(exception.GetBaseException().Message); + await context.Response.WriteAsync(exception.GetBaseException().Message, context.RequestAborted); } } diff --git a/src/__SolutionItems/CommonAssemblyInfo.cs b/src/__SolutionItems/CommonAssemblyInfo.cs index b234f34..ff43995 100644 --- a/src/__SolutionItems/CommonAssemblyInfo.cs +++ b/src/__SolutionItems/CommonAssemblyInfo.cs @@ -4,9 +4,9 @@ [assembly: AssemblyCompany("Luxoft")] [assembly: AssemblyCopyright("Copyright © Luxoft 2024")] -[assembly: AssemblyVersion("1.5.3.0")] -[assembly: AssemblyFileVersion("1.5.3.0")] -[assembly: AssemblyInformationalVersion("1.5.3.0")] +[assembly: AssemblyVersion("1.5.4.0")] +[assembly: AssemblyFileVersion("1.5.4.0")] +[assembly: AssemblyInformationalVersion("1.5.4.0")] #if DEBUG [assembly: AssemblyConfiguration("Debug")]