I spent the better part of a day last week trying to reconfigure our application exception handling. With our switch to a WCF service, our business layer’s method of throwing exceptions for validation problems or as warnings became a bit, well, annoying.
I actually thought I had a solution: Have the WCF service include error details. What that does is send back a SOAP FaultException (System.ServiceModel.FaultException<T>), where T is a System.ServiceModel.ExceptionDetail). From there, I could examine the ExceptionDetail and detemine what type of exception was thrown and handle it appropriately. All well and good, although throwing those exceptions around makes the service somewhat vulnerable, since users can use exceptions to glean information about the system.
I could live with that, except for the fact that my system didn’t work in partial trust. Why? Because System.ServiceModel.ExceptionDetail is not serializable in partial trust (one of its properties doesn’t have a setter or something). So it was back to the drawing board.
A Google search or seven led me to Guy Burstein’s blog post about WCF Exception handling using Microsoft’s Enterprise Library and it’s Exception Handling Application Block. Basically, the MS Enterprise Library has some classes that allow me to automatically wrap and generate a SOAP FaultException with any internal type (T from above) that I want. That means that I can return the data that I want AND I can filter the exceptions as I wish.
It’s a pretty slick system, but getting it working tooks some time. I think most of my time dealt with little idiosyncracies, like where the classes are defined, how the application block is configured, etc. One thing I will not for posterity is that your fault class (The class you want to go into the SOAP exception) cannot be a “class within a class.” I couldn’t figure out the proper full name for that, and it ended up giving me headaches. So I moved the class to the namespace level, and everything worked ok.
I’m not going to repeat all the steps, as Guy’s blog does an excellent job of describing the steps. Once I get some time (ha!), I’d like to look into the automated logging of the exceptions, as I think that would be extremely helpful.