you could do it, but it would probably be better to implement IErrorHandler and add it as a behavior to your service that will allow you to handle unhandled exceptions in one place, so you can create an error exception to return data to users.
ErrorHandler : IErrorHandler { ... just implement the handling of errors here, however you want to handle them }
then to create a behavior that uses this:
Then, if you are a self-hosting, you can simply add the behavior programmatically:
myServiceHost.Description.Behaviors.Add (new ErrorHandlerBehavior ());
if you want to add it through the configuration, you will need one of them:
public class ErrorHandlerElement : BehaviorExtensionElement { public override Type BehaviorType { get { return typeof (ErrorHandlerBehavior); } } protected override object CreateBehavior () { return new ErrorHandlerBehavior (); } } }
and then configuration:
<system.serviceModel> <extensions> <behaviorExtensions> <add name="ErrorLogging" type="ErrorHandlerBehavior, ErrorHandling, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<whatever>" /> </behaviorExtensions> </extensions> <bindings> <basicHttpBinding> <binding name="basicBinding"> </binding> </basicHttpBinding> </bindings> <services> <service behaviorConfiguration="Service1Behavior" name="Service"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicBinding" contract="Service" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="Service1Behavior"> <serviceMetadata httpGetUrl="" httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> <ErrorLogging /> <--this adds the behaviour to the service behaviours --> </behavior> </serviceBehaviors> </behaviors>
source share