WCF: Your Proxy Can Only Fault Once

24. April 2007 18:43

Quite often, one instance of a service proxy will be created and reused for multiple service invocations.  Sometimes this single instance may even be shared across an entire client application.  However, many developers overlook the what happens if a fault occurs between service calls for a proxy that is used more than once.

A proxy can no longer be used for service invocations once a fault has occurred.  This is due to the implementation of the interface ICommunicationObject.  It defines the contract of the state machine for any objects that involve communication within WCF.  This applies to channels, factories, listeners, service hosts, and so forth.  The state machine is responsible for handling the transition of communication state.  For example, it specifies whether the communication object opened, closed, etc.

When a fault occurs, the state of the communication object is set to faulted.  Once a communication object has entered the faulted state, it cannot recover.  This can pose a problem for a proxy instance that is reused.  For example, let's say that a proxy has been instantiated and used to invoke a service operation that results in an error.  This will cause the proxy to transition to a faulted state.  If another service operation is invoked via the same proxy instance, it will result in an exception since the proxy is in an invalid state.

So, how do you avoid this situation? There are a couple of options. 

  1. Subscribe to the Faulted event of the communication object. 
  2. Check the State property of the communication object before executing a service operation.

By using one (or both) of these approaches, it is possible to detect the state of a communication object is faulted and avoid an exception.  Rather than reusing the invalid communication object, a new instance must be created.  It should be emphasized that calling the Close method on an faulted communication object will result in an exception.  To transition a faulted communication object to the closed state, it is necessary to execute the Abort method. 

Comments are closed

About Me

I'm a passionate software developer and advocate of the Microsoft .NET platform.  In my opinion, software development is a craft that necessitates a conscious effort to continually improve your skills rather than falling into the trap of complacency.  I was also awarded as a Microsoft MVP in Connected Systems in 2008, 2009, and 2010.

Can’t code withoutThe best C# & VB.NET refactoring plugin for Visual Studio
Follow jeff_barnes on Twitter

View Jeff Barnes's profile on LinkedIn


Shared Items


Anything you read or see on this site is solely based on my own thoughts.  The material on this site does not necessarily reflect the views of my employer or anyone else.  In other words, I don't speak for anyone other than myself.  So, don't assume I am the official spokesperson for anyone.