Skip to content

Commit 526e4cb

Browse files
committed
fix: add RequestDelegate to ExceptionMiddleware constructor
The middleware was using primary constructor syntax but was missing the RequestDelegate next parameter, causing dependency injection to fail at runtime with InvalidOperationException. Fixed by adding RequestDelegate next as the first parameter in the primary constructor and removing it from InvokeAsync method signature, which is the correct pattern for ASP.NET Core middleware.
1 parent cff7431 commit 526e4cb

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

src/Dotnet.Samples.AspNetCore.WebApi/Middlewares/ExceptionMiddleware.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ namespace Dotnet.Samples.AspNetCore.WebApi.Middlewares;
88
/// <summary>
99
/// Middleware for global exception handling with RFC 7807 Problem Details format.
1010
/// </summary>
11-
public class ExceptionMiddleware(ILogger<ExceptionMiddleware> logger, IHostEnvironment environment)
11+
public class ExceptionMiddleware(
12+
RequestDelegate next,
13+
ILogger<ExceptionMiddleware> logger,
14+
IHostEnvironment environment
15+
)
1216
{
1317
private const string ProblemDetailsContentType = "application/problem+json";
1418

@@ -18,7 +22,7 @@ public class ExceptionMiddleware(ILogger<ExceptionMiddleware> logger, IHostEnvir
1822
/// <summary>
1923
/// Invokes the middleware to handle exceptions globally.
2024
/// </summary>
21-
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
25+
public async Task InvokeAsync(HttpContext context)
2226
{
2327
try
2428
{

0 commit comments

Comments
 (0)