码迷,mamicode.com
首页 > Windows程序 > 详细

ErrorHandling in asp.net web api

时间:2019-01-22 15:51:23      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:roc   oid   contain   .com   from   ges   stop   api   nec   

https://docs.microsoft.com/en-us/aspnet/web-api/overview/error-handling/exception-handling

https://docs.microsoft.com/en-us/aspnet/web-api/overview/error-handling/web-api-global-error-handling

Solution Overview

We provide two new user-replaceable services, IExceptionLogger and IExceptionHandler, to log and handle unhandled exceptions. The services are very similar, with two main differences:

  1. We support registering multiple exception loggers but only a single exception handler.
  2. Exception loggers always get called, even if we‘re about to abort the connection. Exception handlers only get called when we‘re still able to choose which response message to send.

Both services provide access to an exception context containing relevant information from the point where the exception was detected, particularly the HttpRequestMessage, the HttpRequestContext, the thrown exception and the exception source (details below).

 

When to Use

  • Exception loggers are the solution to seeing all unhandled exception caught by Web API.
  • Exception handlers are the solution for customizing all possible responses to unhandled exceptions caught by Web API.
  • Exception filters are the easiest solution for processing the subset unhandled exceptions related to a specific action or controller.

 

 public class ExceptionHandler : IExceptionHandler
    {
        public virtual Task HandleAsync(ExceptionHandlerContext context,
            CancellationToken cancellationToken)
        {
            if (!ShouldHandle(context))
            {
                return Task.FromResult(0);
            }

            return HandleAsyncCore(context, cancellationToken);
        }

        public virtual Task HandleAsyncCore(ExceptionHandlerContext context,
            CancellationToken cancellationToken)
        {
            HandleCore(context);
            return Task.FromResult(0);
        }

        public virtual void HandleCore(ExceptionHandlerContext context)
        {
        }

        public virtual bool ShouldHandle(ExceptionHandlerContext context)
        {
            return context.ExceptionContext.CatchBlock.IsTopLevel;
        }
    }

 

 

 

https://stackoverflow.com/questions/22038800/can-anyone-explain-the-work-flow-of-iexceptionhandler-with-sample-client-applica

CatchBlock.IsTopLevel

IsOutermostCatchBlock does not exists. Use CatchBlock.IsTopLevel instead:

public virtual bool ShouldHandle(ExceptionHandlerContext context)
{
  return context.ExceptionContext.CatchBlock.IsTopLevel;
}

Source on NuDoq: ExceptionHandlerContext and ExceptionContextCatchBlock

 

ErrorHandling in asp.net web api

标签:roc   oid   contain   .com   from   ges   stop   api   nec   

原文地址:https://www.cnblogs.com/chucklu/p/10304032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!