ErrorHandling in asp.net web api
https://docs.microsoft.com/en-us/aspnet/web-api/overview/error-handling/exception-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:
- We support registering multiple exception loggers but only a single exception handler.
- 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();
} return HandleAsyncCore(context, cancellationToken);
} public virtual Task HandleAsyncCore(ExceptionHandlerContext context,
CancellationToken cancellationToken)
{
HandleCore(context);
return Task.FromResult();
} public virtual void HandleCore(ExceptionHandlerContext context)
{
} public virtual bool ShouldHandle(ExceptionHandlerContext context)
{
return context.ExceptionContext.CatchBlock.IsTopLevel;
}
}
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
https://stackoverflow.com/questions/21901808/need-a-complete-sample-to-handle-unhandled-exceptions-using-exceptionhandler-i
You don't need to implement IExceptionHandler low level mechanism yourself.
Instead, you can simply inherit from ExceptionHandler and override the Handle method.
public class MyExceptionHandler : ExceptionHandler
{
public override void Handle(ExceptionHandlerContext context)
{
//TODO: Do what you need to do
base.Handle(context);
}
}
ExceptionHandler implements IExceptionHandler and manage basic core mechanisms (like async and that exception should be handled or not).
Use your exception handler like that:
config.Services.Replace(typeof(IExceptionHandler), new MyExceptionHandler());
ActionFilter中的异常不会被捕获,但是会被外部的api调用知道
public class ValidateModelAttribute : ActionFilterAttribute
{
public override async void OnActionExecuting(HttpActionContext actionContext)
{
//throw an exception here
}
}
这是因为底层的调用中,进行了try catch,所以这个异常是不会被捕获的。
这样做的好处是,action filter出错,不会影响其他的action filter. 并且最终还是会去调用api。只是最后会检查到Error
public virtual Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
try
{
OnActionExecuting(actionContext);
}
catch (Exception ex)
{
return TaskHelpers.FromError(ex);
} return TaskHelpers.Completed();
}
ErrorHandling in asp.net web api的更多相关文章
- Exception Handling in ASP.NET Web API webapi异常处理
原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...
- Global Error Handling in ASP.NET Web API 2(webapi2 中的全局异常处理)
目前,在Web API中没有简单的方法来记录或处理全局异常(webapi1中).一些未处理的异常可以通过exception filters进行处理,但是有许多情况exception filters无法 ...
- 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用
由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...
- ASP.NET Web API Model-ActionBinding
ASP.NET Web API Model-ActionBinding 前言 前面的几个篇幅把Model部分的知识点划分成一个个的模块来讲解,而在控制器执行过程中分为好多个过程,对于控制器执行过程(一 ...
- ASP.NET Web API Model-ParameterBinding
ASP.NET Web API Model-ParameterBinding 前言 通过上个篇幅的学习了解Model绑定的基础知识,然而在ASP.NET Web API中Model绑定功能模块并不是被 ...
- ASP.NET Web API Model-ModelBinder
ASP.NET Web API Model-ModelBinder 前言 本篇中会为大家介绍在ASP.NET Web API中ModelBinder的绑定原理以及涉及到的一些对象模型,还有简单的Mod ...
- ASP.NET Web API Model-ValueProvider
ASP.NET Web API Model-ValueProvider 前言 前面一篇讲解了Model元数据,Model元数据是在Model绑定中很重要的一部分,只是Model绑定中涉及的知识点比较多 ...
- ASP.NET Web API Model-ModelMetadata
ASP.NET Web API Model-ModelMetadata 前言 前面的几个篇幅主要围绕控制器的执行过程,奈何执行过程中包含的知识点太庞大了,只能一部分一部分的去讲解,在上两篇中我们看到在 ...
- ASP.NET Web API 过滤器创建、执行过程(二)
ASP.NET Web API 过滤器创建.执行过程(二) 前言 前面一篇中讲解了过滤器执行之前的创建,通过实现IFilterProvider注册到当前的HttpConfiguration里的服务容器 ...
随机推荐
- PAT Advance 1020
题目: 1020. Tree Traversals (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue S ...
- hdu1066(经典题)
求N个数阶乘末尾除0后的数值. 主要的难点在于要把这个N个数所含的2和5的队数去掉. 网上方法很多很好. 不多说 Last non-zero Digit in N! Time Limit: 2000/ ...
- IntelliJ IDEA 工具技巧
IntelliJ IDEA 工具技巧 以下都是自己积累的IntelliJ IDEA 使用技巧,比较零碎,观看不便之处还望海涵,如有错误之处还望指正 自己常用,不懂的可以加群询问:244930845 S ...
- IIS 部署WCF时遇到这么个错:
转(http://blog.csdn.net/vic0228/article/details/48806405) 部署WCF时遇到这么个错: "The service cannot be a ...
- python基础之类的内置__setattr__,__delattr__,__getattr__和 二次加工标准类型(包装)
一.内置attr:__setattr__,__delattr__,__getattr__ __setattr__ #添加/修改属性会触发它的执行 __delattr__ #删除属性的时候会触发 __g ...
- Milking Time---poj3616(简单dp)
题目链接:http://poj.org/problem?id=3616 题意:人从奶牛身上挤奶有m个时间段(1----n),每个时间段包含 s e f 表示从 s 到 e 的这段时间可以获得 f 单位 ...
- 007-组件和Props
一.概述 组件让你可以将用户界面分成独立的,可重复使用的部分,并且可以独立思考每个部分. 从概念上讲,组件就像JavaScript函数一样.他们接受任意输入(称为“props”)并返回描述屏幕上应显示 ...
- python中decorator的用法及原理(一)
0. 概念 什么叫装饰器,其实也可以叫做包装器.即对于一个既有的函数func(args),在调用它之前和之后,我们希望都做一些事情,把这个函数包装起来. Python中的装饰器分为两类:函数装饰器和类 ...
- [设计模式]访问者 Visitor 模式
访问者模式是对象的行为模式. 访问者模式的目的是封装一些施加于某种数据结构元素之上的操作.一旦这些操作需要修改的话,接受这个操作的数据结构则可以保持不变.
- oracle external密码验证
什么是external密码验证 当OS user 中存在和DB user 同名的用户时 直接使用和DB user 同名的OS user 可以不输入密码直接登录数据库, [oracle@zxrac1 ...