C# – WCF Error The communication object, System.ServiceModel.Channels.ServiceChanne, cannot be used for communication because it is in the Faulted state

cwcf

We get

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

message when we close the application. Can anyone tell me how to fix it? We know it is communication channel trying to close but it is not able to close due to service not available or in faulted state.

All I can say is, when the service is not available, but the Garbage collector trying to destroy the object, the communication objects is calling its service Close function. There we get exception.

Best Answer

When you ask a question about an exception, you should post the entire exception, including all InnerException instances. You should catch the exception, display ex.ToString(), then rethrow the exception with "throw":

try {
    // Do whatever causes the exception
} catch (Exception ex) {
    Console.WriteLine(ex.ToString());  // Or Debug.Print, or whatever
    throw; // So exception propagation will continue
}

In this case, I wonder if you have a using block around your proxy instantiation:

using (var proxy = new WcfProxyClient())
{
    // Use of proxy
}

There is a design flaw in WCF that makes this about the only place in .NET where you should not use a using block. Instead, you need to do it by hand. See http://web.archive.org/web/20100703123454/http://old.iserviceoriented.com/blog/post/Indisposable+-+WCF+Gotcha+1.aspx.

Also, see "What is the best workaround for the WCF client using block issue?" and "Indisposable WCF clients".