Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle WCF exceptions (consolidated list with code)
    text
    copied!<p>I'm attempting to extend <a href="https://stackoverflow.com/questions/573872/what-is-the-best-workaround-for-the-wcf-client-using-block-issue/573925#573925">this answer on SO</a> to make a WCF client retry on transient network failures and handle <a href="https://stackoverflow.com/questions/479618/renewing-a-wcf-client-when-sct-has-expired">other situations that require a retry such as authentication expiration.</a></p> <p><strong>Question:</strong></p> <p>What are the WCF exceptions that need to be handled, and what is the correct way to handle them? </p> <p>Here are a few sample techniques that I'm hoping to see instead of or in addition to <code>proxy.abort()</code>:</p> <ul> <li>Delay X seconds prior to retry</li> <li>Close and recreate a New() WCF client. Dispose the old one.</li> <li>Don't retry and rethrow this error</li> <li>Retry N times, then throw</li> </ul> <p>Since it's unlikely one person knows all the exceptions or ways to resolve them, do share what you know. I'll aggregate the answers and approaches in the code sample below.</p> <pre><code> // USAGE SAMPLE //int newOrderId = 0; // need a value for definite assignment //Service&lt;IOrderService&gt;.Use(orderService=&gt; //{ // newOrderId = orderService.PlaceOrder(request); //} /// &lt;summary&gt; /// A safe WCF Proxy suitable when sessionmode=false /// &lt;/summary&gt; /// &lt;param name="codeBlock"&gt;&lt;/param&gt; public static void Use(UseServiceDelegateVoid&lt;T&gt; codeBlock) { IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel(); bool success = false; try { codeBlock((T)proxy); proxy.Close(); success = true; } catch (CommunicationObjectAbortedException e) { // Object should be discarded if this is reached. // Debugging discovered the following exception here: // "Connection can not be established because it has been aborted" throw e; } catch (CommunicationObjectFaultedException e) { throw e; } catch (MessageSecurityException e) { throw e; } catch (ChannelTerminatedException) { proxy.Abort(); // Possibly retry? } catch (ServerTooBusyException) { proxy.Abort(); // Possibly retry? } catch (EndpointNotFoundException) { proxy.Abort(); // Possibly retry? } catch (FaultException) { proxy.Abort(); } catch (CommunicationException) { proxy.Abort(); } catch (TimeoutException) { // Sample error found during debug: // The message could not be transferred within the allotted timeout of // 00:01:00. There was no space available in the reliable channel's // transfer window. The time allotted to this operation may have been a // portion of a longer timeout. proxy.Abort(); } catch (ObjectDisposedException ) { //todo: handle this duplex callback exception. Occurs when client disappears. // Source: https://stackoverflow.com/questions/1427926/detecting-client-death-in-wcf-duplex-contracts/1428238#1428238 } finally { if (!success) { proxy.Abort(); } } } </code></pre>
 

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