Last post Aug 10, 2015 10:11 PM by Krunal Parekh
Aug 07, 2015 04:12 PM|kalcyn|LINK
I am trying to generate custom error response for unhandled exceptions that could occur in WebApiConfig as part of implementing a service layer using webapi on IIS 7.5 integrated mode.
Already have a custom ExceptionFilterAttribute to catch exceptions coming from controller.
Tried custom ExceptionHandler and ExceptionLogger to catch exceptions coming from code paths outside of controllers, but exception from WebApiConfig doesn't come here. They are going to Application_Error in Global.asax.
Implemented the custom error handling in Application_Error in Global.asax but Response object is not available to write back to client as I am using integrated mode in IIS.
I also tried implementing custom HttpModule. Though the exception reaches here, I am having same "Response object not available in this context" error as in Global.asax.
My last option was to implement MessageHandler, which also didn't work. The exception didn't reach the handler though the registration for it was done well before the code that was throwing exception within webapiconfig.
As I am developing a service, I do not want to serve any static pages. Logging is also not a problem. But I am unable to generate a response back to the client.
Please help me!
Aug 10, 2015 10:11 PM|Krunal Parekh|LINK
"there’s no easy way in Web API to log or handle errors globally. Some unhandled exceptions can be processed via
exception filters, but there are a number of cases that exception filters can’t handle. For example:
There are two major cases for handling exceptions, the case where we are able to send an error response and the case where all we can do is log the exception. An example for the latter case is when an exception is thrown in the middle of streaming response
content; in that case it is too late to send a new response message since the status code, headers, and partial content have already gone across the wire, so we simply abort the connection. Even though the exception can’t be handled to produce a new response
message, we still support logging the exception."
I am not sure about the response as I have not tested this my self but you can at least log the exceptions when error is thrown during above mentioned scenarios.