I have many webservices running in my project but something odd has been happening for quite some time. My services occasionally crash for no reason with an error message "The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state." This usually happens when I run the application first thing in the morning after which they occur less frequently. Any ideas as to what might be causing this error?
-
Did you try WCF tracing?– Ladislav MrnkaAug 24, 2010 at 7:41
-
Is this error coming from code consuming a WCF service, or from the service itself?– Graham ClarkAug 24, 2010 at 8:35
-
@Ladislav: No, not yet. I am not too sure about WCF tracing. Ne good tutorials to get me starteD? @Graham: This is basically coming from the code consuming the WCF service– FaraxAug 25, 2010 at 2:43
-
1Hero you have descriptions about setting WCF tracing and using SvcTraceViewer to browse tracing data. Set the tracing in the service host project. Tracing: msdn.microsoft.com/en-us/library/ms733025.aspx SvcTraceViewer: msdn.microsoft.com/en-us/library/ms732023.aspx– Ladislav MrnkaAug 25, 2010 at 7:25
2 Answers
If a WCF service throws a FaultException
, the client will have its state changed to CommunicationState.Faulted
. If you then try to use this client object to call another service operation, you'll get the error
"The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state."
You might also get this error if you try to call the Close()
method on a faulted client, I can't remember.
You can check the state of your client object by checking the State
property. If you want to close your client properly (which you should be doing), you need to call the Abort()
method if the client is in the Faulted
state, and the Close()
method if the client is in any other state.
-
Kind of defeating the purpose of
using
, doesn't it? WCF never stops amazing me of how badly designed it is. Jan 19, 2015 at 12:27
This sounds like a timeout combined with not handling the failing services.
It takes longer to run in the morning since code has to be JIT compiled, maybe also database needs to cache data and query plans.
Here is one way to get the WCF client to clean up after itself http://nimtug.org/blogs/damien-mcgivern/archive/2009/05/26/wcf-communicationobjectfaultedexception-quot-cannot-be-used-for-communication-because-it-is-in-the-faulted-state-quot-messagesecurityexception-quot-an-error-occurred-when-verifying-security-for-the-message-quot.aspx
-
-
You still have to close the service after you have used it, and you need to abort the service if it is in faulted state Aug 25, 2010 at 7:05
-
Indeed after removing my USING block I could see the real error. 10x Jun 12, 2013 at 12:07