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.

Nella programmazione SOA oriented, uno dei principali errori che si commette nell’utilizzo dei servizi WCF e creare un’istanza del Transparent Proxy all’interno dello statement using, come segue:

using(ServiceReference.ClientType client = new ServiceReference.ClientType(“EndpointAddress”))
  {
      //faccio qualcosa

  } // <– Questa linea potrebbe generare un errore

L’errore che si riceve è il seguente:

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

Questo errore si ottiene, in generale, quando il servizio è in uno stato di Fault (guasto), quindi non accessibile, ma nella maggior parte dei casi quando non è stato gestito correttamente il Dispose del proxy client, per cui le chiamate successive alla prima troveranno il servizio nello stato di Fault.

Per una corretta gestione della distruzione (Dispose) del proxy client utilizzare lo statement Try Catch arricchito con il Finally, come indicato di seguito:

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();
	}
}

E’ possibile trovare riferimenti puntuali sull’MSDN al seguente articolo: Evitare problemi con l’uso dello statement using.

3 commenti

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.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *