I have a net8 Microservice written in c#.I want to use the IExceptionHandler Middleware but the Handler ist never called and the exception is returned to the Endpoint-Call.
The Handler:
public class GlobalExceptionHandler : IExceptionHandler{ public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken) { var statusCode = exception.GetStatusCode(); httpContext.Response.ContentType = "text/plain; charset=utf-8"; httpContext.Response.StatusCode = statusCode; var message = (exception.Message.EndsWith('"') && exception.Message.StartsWith('"')) ? exception.Message : $"\"{exception.Message}\""; await httpContext.Response.WriteAsync(message, cancellationToken); return true; }}
The Startup:
public void ConfigureServices(IServiceCollection services){ #region REST services .AddControllers(options => { options.Filters.Add<ModifyIdActionFilter>(); options.Conventions.Add(new ApiConventions()); }) .AddJsonOptions(options => { options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); }); services.AddExceptionHandler<GlobalExceptionHandler>(); services.AddProblemDetails(); #endregion}public void Configure(IApplicationBuilder app, IWebHostEnvironment env){ #region Routing & Middleware app.UseRouting(); app.UseExceptionHandler(); #endregion}
What am I missing?