How to make sure that you do not get an exception from the Faulted WCF state?

I get this exception:

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

The WCF service uses wsHttpBinding by default. I use WCF as follows, wherever I use:

using (var proxy = new CAGDashboardServiceClient()) { proxy.Open(); var result = proxy.GetSiteForRegion(ddlRegions.SelectedValue); ddlSites.DataSource = result; ddlSites.DataBind(); proxy.Close(); } 

The error line shown in the message seems to be after the last proxy. close. Not sure what is going on. I start the service from visual studio 08.

Here is the trace information:

 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 CAGDashboard.UserControls.ucVolunteerCRUDGrid.ddlRegions_SelectedIndexChanged(Object sender, EventArgs e) in C:\Documents and Settings\rballalx\My Documents\Visual Studio 2008\Projects\DashboardCAG\CAGDashboard\UserControls\ucVolunteerCRUDGrid.ascx.cs:line 81 at System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) at System.Web.UI.WebControls.DropDownList.RaisePostDataChangedEvent() at System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() at System.Web.UI.Page.RaiseChangedEvents() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
+46
exception wcf wcf-client
Feb 10 '09 at 1:04
source share
5 answers
+60
Feb 10 '09 at 5:23
source share



Update

This related answer describes a simpler and simpler way to do the same with C # syntax.




Original post

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

See Expected Exceptions for details.

 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 in 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 Sample: C # or VB

Given that there are so many problems with using the using operator , (heated?) Internal discussions and topics on this issue, I am not going to waste my time trying to become a code cowboy and find a cleaner way. I just suck it up and implement this detailed (but reliable) way for my server applications to WCF clients.

+19
Feb 19 2018-11-11T00:
source share

If the transfer mode is Buffered , then verify that the values โ€‹โ€‹of MaxReceivedMessageSize and MaxBufferSize are the same . I simply resolved the issue with the state question in this way after I grabbed it for several hours and thought that I would post it here if it helps someone.

+5
Apr 24 '14 at 2:38
source share

This error can also be caused by null methods marked with the OperationContract attribute. It was my problem when creating a new service and its long testing.

+1
Apr 21 '15 at 15:53
source share

Like Ryan Rodemeyerโ€™s answer, I found that when the UriTemplate in the Contract is invalid, you can get this error. In my case, I used the same parameter twice. For example:

 /Root/{Name}/{Name} 
0
Apr 13 '16 at 19:07
source share



All Articles