Add dependency injection for global exception handler
Introduce methods in DependencyInjection.cs for configuring and using a global exception handler. Update the project file to include a reference to Microsoft.Extensions.Options. Modify GlobalExceptionHandlerMiddleware to accept IOptions<GlobalExceptionHandlerOptions> for enhanced configuration through dependency injection.
This commit is contained in:
parent
cb7b69a0a2
commit
97695fb0b0
@ -1,12 +1,18 @@
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace DigitalData.Core.Exceptions.Middleware;
|
namespace DigitalData.Core.Exceptions.Middleware;
|
||||||
|
|
||||||
public static class DependencyInjection
|
public static class DependencyInjection
|
||||||
{
|
{
|
||||||
|
public static IServiceCollection ConfigureGlobalExceptionHandler(this IServiceCollection services, Action<GlobalExceptionHandlerOptions> options)
|
||||||
|
{
|
||||||
|
return services.Configure(options);
|
||||||
|
}
|
||||||
|
|
||||||
public static IApplicationBuilder UseGlobalExceptionHandler(this IApplicationBuilder app)
|
public static IApplicationBuilder UseGlobalExceptionHandler(this IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
app.UseMiddleware<GlobalExceptionHandlerMiddleware>();
|
app.UseMiddleware<GlobalExceptionHandlerMiddleware>();
|
||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -6,6 +6,10 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\DigitalData.Core.Exceptions\DigitalData.Core.Exceptions.csproj" />
|
<ProjectReference Include="..\DigitalData.Core.Exceptions\DigitalData.Core.Exceptions.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
@ -13,17 +14,21 @@ using System.Text.Json;
|
|||||||
public class GlobalExceptionHandlerMiddleware
|
public class GlobalExceptionHandlerMiddleware
|
||||||
{
|
{
|
||||||
private readonly RequestDelegate _next;
|
private readonly RequestDelegate _next;
|
||||||
|
|
||||||
private readonly ILogger<GlobalExceptionHandlerMiddleware>? _logger;
|
private readonly ILogger<GlobalExceptionHandlerMiddleware>? _logger;
|
||||||
|
|
||||||
|
private readonly GlobalExceptionHandlerOptions? _options;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="GlobalExceptionHandlerMiddleware"/> class.
|
/// Initializes a new instance of the <see cref="GlobalExceptionHandlerMiddleware"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="next">The next middleware in the request pipeline.</param>
|
/// <param name="next">The next middleware in the request pipeline.</param>
|
||||||
/// <param name="logger">The logger instance for logging exceptions.</param>
|
/// <param name="logger">The logger instance for logging exceptions.</param>
|
||||||
public GlobalExceptionHandlerMiddleware(RequestDelegate next, ILogger<GlobalExceptionHandlerMiddleware>? logger = null)
|
public GlobalExceptionHandlerMiddleware(RequestDelegate next, ILogger<GlobalExceptionHandlerMiddleware>? logger = null, IOptions<GlobalExceptionHandlerOptions>? options = null)
|
||||||
{
|
{
|
||||||
_next = next;
|
_next = next;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_options = options?.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user