I have an ASP.NET Core 6.0 Web API. One of the middleware catches an exception and translates it to a user-friendly error message as response.
This is existing web API and multiple middleware are added and changes by multiple developer, I am not sure which middleware is causing problem to share minimal reproducible example. when API response is 200 there is no issue. Only when response is != 200, client will continuously wait for response and in Application Insight Request table (logged by custom middleware) there are multiple rows created for same request values. Below code form middleware is only one, i can think of causing issue.
Looking for help in understanding if there is any config/feature in ASP.NET which internally can retrigger new request in case of errors Or if there is any Http status (like http redirection) which API might be sending which might be making client to send another request. Or any suggestion on how to debug/troubleshoot it.
app.UseMiddleware(middlewareA);
app.UseMiddleware(middlewareB);
//middlewareB is implemented as
public async Task Invoke(HttpContext context)
{
try
{
//call next middleware
}
catch(Exception ex)
{
context.Response.StatusCode = 404;
context.Response.WriteAsync("custom error message");
}
}
app.UseMiddleware(middlewareA); app.UseMiddleware(middlewareB);you can check the application log in order to know the details exactly which middleware causing the issue.