简体   繁体   English

所有Web API异步等待方法的全局try catch处理程序

[英]Global try catch handler for all the web api async await methods

I am aware of the fact that await operator is clean and if used with async/await all the way down it will throw the exceptions properly. 我知道一个事实,即等待操作符是干净的,并且如果一直与async / await一起使用,它将正确地引发异常。

I have a bulk of API calls (Web API) written in controllers which uses async await all the way down. 我在控制器中编写了大量的API调用(Web API),这些调用一直使用async等待。 Is there any way to write a common try catch for all those async await methods or do I need to go for a harder way to add try catch in each of those API methods to handle all the unhandled exceptions. 是否有任何方法可以为所有这些异步等待方法编写通用的try catch,还是我需要采用更困难的方法在每个API方法中添加try catch来处理所有未处理的异常。

You can write an exception filter . 您可以编写一个异常过滤器

Note that exception filters apply whether or not the method is async . 请注意,无论方法是否async应用异常过滤器。 The same exception filter will work fine for both synchronous and asynchronous methods that throw exceptions. 对于抛出异常的同步方法和异步方法,相同的异常过滤器都可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM