I have a lot of legacy code that is now a backend for a WCF REST service - it used to be a usual WCF service backend before, if that matters. I want to implement a mechanism that would catch any exception in any method and analyze it. If it turns out to be a known error, it will be processed and turned into a friendly-looking fault.
I know that I can throw FaultException
or WebProtocolException
instead of 'usual' exceptions, but there are lots of places where exceptions are thrown all over the code, and looking for all of them is quite a painful option.
I tried to add an endpoint behavior extension that creates a new behavior which overrides standard WebHttpBehavior.AddServerErrorHandlers
method and adds my error handlers (IErrorHandler
implementations) to the endpoint dispatcher error handlers collection. Inside the error handlers I analyze the exception and create (or do not create) a desired fault basing on this exception.
I expected this mechanism to return custom data for any known exception, but I was wrong. Good old Microsoft has implemented a wonderful inevitable WebHttpBehavior2
, which unconditionally adds an internal Microsoft.ServiceModel.Web.WebErrorHandler
into the end of endpoint dispatcher error handlers collection. This handler ignores all previously executed handlers and recognizes only a small set of exceptions, while the majority is interpreted as "Internal Server Error", and nothing more.
The question is whether I am on a right path and there is a way to disable this handler in WCF REST mechanism, or introduce it with a new Exception (e.g., when any exception is caught, it is first processed by my handlers and if they throw/return, for instance, FaultException, then this new exception is supplied to Microsoft.ServiceModel.Web.WebErrorHandler
instead of the original one). If all my experiments with IErrorHandler
and behavior extensions are worthless, what is an alternative? Again, I really do not want to modify the exception throwing logic, I want one place to catch the exceptions and process them.
Thanks a lot!