19

When i try use a webservice i get the following exception. My main question is when does this exception happen? on the server or client? where is the error? Does the server throw this for a wide range of faults?

I did some changes on my own that seems to work

It actually works now. I removed using and added som cleanup on the service client.

if (Service != null && Service.State != CommunicationState.Faulted)
                {
                    success = true;
                    Service.Close();
                }

            }
            catch (Exception ex)
            {
                msg = "Error" + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace;
            }
            finally{
                if (!success)
                {
                    if (Service != null) Service.Abort();
                }
            }

This was the exception:

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

Server stack trace: 
at System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout)

Exception rethrown at [0]: 
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
 at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout)
at System.ServiceModel.ClientBase`1.System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout)
at System.ServiceModel.ClientBase`1.Close()
at System.ServiceModel.ClientBase`1.System.IDisposable.Dispose()
at bNet.Services.Customers.Cres.Helios.ServiceForm.Send(ServiceFormAction task) in C:\bNetProjects\bNet Web Tools\Solution root\bNet.Services\Customers\Cres\Helios\ServiceForm.cs:line 99
at bNet.Web.Sites.Public.Customers.Cres.ServiceSkjema.Units.Page.ServiceFormControl.SubmitFormClick(Object sender, EventArgs e) in C:\bNetProjects\bNet Web Tools\Solution root\bNet.Web.Sites.Public\Customers\Cres\ServiceSkjema\Units\Page\ServiceFormControl.ascx.cs:line 192
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
espvar
  • 1,045
  • 5
  • 16
  • 28
  • The exception call stack in fact tells you a lot that you must pay attention to. For example, Dispose calls Close. Therefore, if the proxy is already in Faulted state, you can only call Abort, not Close nor Dispose. Luckily you find out the necessary change. Microsoft in fact has a nice article that emphasizes on what kind of calling pattern is recommended, http://msdn.microsoft.com/en-us/library/aa355056.aspx – Lex Li Feb 23 '12 at 08:40
  • Take a look here as well http://stackoverflow.com/questions/2763592/the-communication-object-system-servicemodel-channels-servicechannel-cannot-be – NoWar Feb 26 '14 at 15:26
  • Also got exactly this exception not because exception handling, but just for trying to return `DataTable` without `TableName` set. See http://stackoverflow.com/questions/12702/returning-datatables-in-wcf-net – Vojtěch Dohnal Jan 14 '16 at 09:13
  • Possible duplicate of [The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication](https://stackoverflow.com/questions/2763592/the-communication-object-system-servicemodel-channels-servicechannel-cannot-be) – Stephen Kennedy Oct 22 '18 at 10:40

3 Answers3

18

Faulted state means there has been an unexpected exception on the server side. In an earlier call.

You should have gotten an exception at the client side too, maybe your code ignores it?

You can solve it by reopening the connection. But it seems you need better error handling.

H H
  • 263,252
  • 30
  • 330
  • 514
10

Instead of using the using statement, try running your code without it.

From

using(var client = new WCFClient())
{
    // ... code
}

to

var client = new WCFClient()

// ... code

Upon doing so, we were able to see that the original WCF Cannot be used for communication because it is in the Faulted state message was caused by the using() call itself. Why? Our code that was using the WCF client was passing in invalid credentials, and the server responded with an error and changing the state of the proxy to faulted. The using() block, as we know, calls Dispose() on the object - in this case our WCF client.

Because the WCF client failed, and the WCF client was in a faulted state, calling Dispose() caused the error WCF Cannot be used for communication because it is in the Faulted state to be thrown.

We were able to see this by wrapping the code that uses the WCF client in a try...catch block.

reZach
  • 8,945
  • 12
  • 51
  • 97
  • 1
    Kudos @Zac, removing using helped us catch the appropriate exception which happened to be: "The client certificate is not provided. Specify a client certificate in ClientCredentials.". Seems removing using should be the first thing to do to get the exact exception than the generic: "The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state." – DiligentKarma Nov 17 '21 at 01:13
  • Same here. Only after removing the using block could I see more details about the error. – PDoria Dec 21 '22 at 17:42
2

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.

Ryan Rodemoyer
  • 5,548
  • 12
  • 44
  • 54