Search code examples
c#async-await.net-4.6.1

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.

I have a bulk of API calls (Web API) written in controllers which uses async await all the way down. 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.


Solution

  • You can write an exception filter.

    Note that exception filters apply whether or not the method is async. The same exception filter will work fine for both synchronous and asynchronous methods that throw exceptions.