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

cnetservicewcf

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)

Best Answer

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.