using global exception handeling messes up DelegatingHandler

Deadly 提交于 2019-12-08 06:39:29

Dont use ExceptionHandler as base class, implement interface IExceptionHandler

public class WebAPiExceptionHandler : IExceptionHandler
{
    public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
    {
        var fout = new ErrorInformation
        {
            Message = "Iets is misgegaan"
            , ErrorDate = DateTime.UtcNow
        };

        var httpResponse = context.Request.CreateResponse(HttpStatusCode.InternalServerError, fout);

        context.Result = new ResponseMessageResult(httpResponse);

        return Task.FromResult(0);
    }

    private class ErrorInformation
    {
        public string Message { get; set; }
        public DateTime ErrorDate { get; set; }
    }
}

The problem is that ExceptionHandler only executes Handle(ExceptionHandlerContext context) method if ShouldHandle(ExceptionHandlerContext context) returns true.

Overriding bool ShouldHandle(ExceptionHandlerContext context) to always return true fix the problem for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!