post icon

The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

In the SOA-oriented programming, one of the major mistakes that you make when use WCF services is create an instance of the Transparent Proxy statements within the using statement, as follows:

using(ServiceReference.ClientType client = new ServiceReference.ClientType(“EndpointAddress”))
  {
  //do stuff

 } // <– this line might throw

The error you get is the following:

Type: System.ServiceModel.CommunicationObjectFaultedException.
Message: The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

In general, you obtain this error when the service is in a Fault State, so not accessible, but in most cases when the client proxy wasn’t handled properly disposed, so subsequent calls to the first will find the service in faulted state.

To ensure the right management of client proxy disposing, you have to use the Try Catch statement enriched with Finally, as shown below:

ServiceReference.ClientType client = null;

try
{
	client = new ServiceReference.ClientType("EndpointAddress");
	client.Open();

	//do stuff
}
catch (Exception e)
{
	//do stuff
}
finally
{
	if(client != null)
	{
		if (client.State == CommunicationState.Opened)
			client.Close();
		else if (client.State == CommunicationState.Faulted)
			client.Abort();
	}
}

You can find specific references to the following article sull’MSDN: Avoiding Problems with the Using Statement.

November 30th, 2011 @ 01:14

3 Comments

Leave a comment
  1. Marco
    December 1st, 2011 at 08:53 #

    Quanti hanno commesso quest’errore!
    Anche io ci sono cascato 😀

    • Nicola Celiento
      January 26th, 2012 at 09:57 #

      Io per primo, ecco il motivo di questo post! 😉

  2. Michele C. Keith
    October 2nd, 2013 at 12:55 #

    You get this error because you let a .NET exception happen on your server side, and you didn’t catch and handle it, and didn’t convert it to a SOAP fault, either.

Leave a Reply

Are you human? Time limit is exhausted. Please reload CAPTCHA.