WCF Cannot be used for communication because it is

2019-04-05 23:07发布

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)

2条回答
干净又极端
2楼-- · 2019-04-05 23:16

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.

查看更多
Summer. ? 凉城
3楼-- · 2019-04-05 23:18

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.

查看更多
登录 后发表回答