Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4135

ExceptionHandler is registered but never entered on occuring Exception

$
0
0

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?


Viewing all articles
Browse latest Browse all 4135

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>