Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling WCF exceptions/faults along with FaultException AND IErrorHandler
    text
    copied!<p>I'm interested in implementing a proper exception handling for my WCF application. After a quick google ride, I've come across two solutions.</p> <ol> <li>Providing <code>FaultExceptions</code> for the client.</li> <li>General exception handling using <code>IErrorHandler</code>.</li> </ol> <p>What I didn't found, though, was a practical example that combines the two methods - <em>if this is even considered as good practice</em>.</p> <p>My thoughts:</p> <p><strong>Throwing FaultExceptions with own detail types</strong></p> <pre><code>throw new FaultException&lt;StackOverflowFault&gt;( new StackOverflowFault(StackOverflowFaultCode.Rules, "This question is not considered as constructive.")); </code></pre> <p><strong>Catching unhandled exceptions with <code>IErrorHandler</code> and log them.</strong></p> <pre><code>public bool HandleError(Exception ex) { try { SomeLogger.Error(ex.Message, ex.Source); return true; } catch (Exception) { // Well, there was an exception handling the // exception :( // -&gt; Drop the exception and throw a new one throw new Exception("Unknown exception occured."); } } </code></pre> <p><strong>...and provide faults based on the exception type</strong></p> <p><em>(not everything is for the clients eyes!)</em></p> <pre><code>public void ProvideFault(Exception error, MessageVersion version, ref Message fault){ if(error is FaultException) { fault = Message.CreateMessage(version, ((FaultException&lt;StackOverflowFault&gt;)error).Reason) } else if(error is SqlException) { // What would Jon Skeet do? } } </code></pre> <hr> <p><strong>My question</strong></p> <p>Is this considered as okay practice? And: If I'm already throwing FaultException in the application that are suitable for the client - does it makes sense to let them be handled by <code>IErrorHandler</code> (<em>which does it automatically</em>)?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload