I am having a global Exception handler in my web api project. This works fine except when the exception is raised through a lambda expression. I have provided a sample code below:
[HttpGet]
public IHttpActionResult Test()
{
//Throw new Exception();// this exception is handled by my ExceptionHandler
var list = new List<int>();
list.Add(1);
IEnumerable<int> result = list.Select(a => GetData(a));
return Ok(result);
}
private static int GetData(int a)
{
throw new Exception();//This is not handled by my global exception handler
}
This is my global Exception Handler
public class ExceptionHandlerAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
//Do something
}
}
I register it in my WebApiConfig class
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "Get", id = RouteParameter.Optional }
);
config.Filters.Add(new ExceptionHandlerAttribute());
}
}