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

前端 未结 14 1656
我寻月下人不归
我寻月下人不归 2020-12-04 05:43

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

14条回答
  •  情深已故
    2020-12-04 06:33

    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

提交回复
热议问题