espvar
espvar

Reputation: 1065

WCF Cannot be used for communication because it is in the Faulted state

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)

Upvotes: 21

Views: 90216

Answers (3)

reZach
reZach

Reputation: 9459

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.

Upvotes: 11

Ryan Rodemoyer
Ryan Rodemoyer

Reputation: 5692

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.

Upvotes: 2

Henk Holterman
Henk Holterman

Reputation: 273274

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.

Upvotes: 19

Related Questions