Search code examples
c#wcf

The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication


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

What is this error all about, and how would I go about solving it?


Solution

  • 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.

    Now since the server side "bombed" out, the WCF runtime has "faulted" the channel - e.g. the communication link between the client and the server is unusable - after all, it looks like your server just blew up, so you cannot communicate with it any more.

    So what you need to do is:

    • always catch and handle your server-side errors - do not let .NET exceptions travel from the server to the client - always wrap those into interoperable SOAP faults. Check out the WCF IErrorHandler interface and implement it on the server side

    • if you're about to send a second message onto your channel from the client, make sure the channel is not in the faulted state:

      if(client.InnerChannel.State != System.ServiceModel.CommunicationState.Faulted)
      {
         // call service - everything's fine
      }
      else
      {
         // channel faulted - re-create your client and then try again
      }
      

      If it is, all you can do is dispose of it and re-create the client side proxy again and then try again