How to make sure you don't get WCF Faulted state exception?

前端 未结 5 522
鱼传尺愫
鱼传尺愫 2020-12-12 13:09

I am getting this exception:

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

相关标签:
5条回答
  • 2020-12-12 13:32

    Similar to Ryan Rodemoyer's answer, I found that when the UriTemplate on the Contract is not valid you can get this error. In my case, I was using the same parameter twice. For example:

    /Root/{Name}/{Name}
    
    0 讨论(0)
  • 2020-12-12 13:34

    You should avoid putting client proxies in using blocks.

    0 讨论(0)
  • 2020-12-12 13:38

    This error can also be caused by having zero methods tagged with the OperationContract attribute. This was my problem when building a new service and testing it a long the way.

    0 讨论(0)
  • 2020-12-12 13:44

    If the transfer mode is Buffered then make sure that the values of MaxReceivedMessageSize and MaxBufferSize is same. I just resolved the faulted state issue this way after grappling with it for hours and thought i'll post it here if it helps someone.

    0 讨论(0)
  • 2020-12-12 13:51

    Update:

    This linked answer describes a cleaner, simpler way of doing the same thing with C# syntax.


    Original post

    This is Microsoft's recommended way to handle WCF client calls:

    For more detail see: Expected Exceptions

    try
    {
        ...
        double result = client.Add(value1, value2);
        ...
        client.Close();
    }
    catch (TimeoutException exception)
    {
        Console.WriteLine("Got {0}", exception.GetType());
        client.Abort();
    }
    catch (CommunicationException exception)
    {
        Console.WriteLine("Got {0}", exception.GetType());
        client.Abort();
    }
    

    Additional information

    So many people seem to be asking this question on WCF that Microsoft even created a dedicated sample to demonstrate how to handle exceptions:

    c:\WF_WCF_Samples\WCF\Basic\Client\ExpectedExceptions\CS\client

    Download the sample: C# or VB

    Considering that there are so many issues involving the using statement, (heated?) Internal discussions and threads on this issue, I'm not going to waste my time trying to become a code cowboy and find a cleaner way. I'll just suck it up, and implement WCF clients this verbose (yet trusted) way for my server applications.

    0 讨论(0)
提交回复
热议问题