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

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.

3 COMMENTS

comments user
Marco

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

    comments user
    Nicola Celiento

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

comments user
Michele C. Keith

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

Your email address will not be published. Required fields are marked *

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.