I have a WCF service which logs any exceptions and then throws them as FaultExceptions.
I am doing a lot of repetition e.g. in each service method.
try {
// do some work
}
catch(Exception ex)
{
Logger.log(ex);
// actually will be Fault Exception but you get the idea.
throw ex;
}
I am looking for a more elegant way to do this as I am cutting and pasting the try/catch throughout each service.
Is there a design pattern /C# trick that could be used to make this more elegant?
We have a similar problem in one of our WCF services, which I've solved by using a helper delegate:
Usage:
You still have to repeat the wrapper, but that's a lot less code and you can modify the logic in the
try..catch
in one place.If your question is about how to make your current pattern faster for you to work with you can repeat that boiler plate code by creating a
Snippet
You may want to try AspectF (not mine): http://www.codeproject.com/Articles/42474/AspectF-Fluent-Way-to-Add-Aspects-for-Cleaner-Main. It's by Omar Al-Zabir... creator of Dropthings framework, amongst other things. Hope this helps you.