50

我得到了这个例外:

通信对象 System.ServiceModel.Channels.ServiceChannel 不能用于通信,因为它处于故障状态。

WCF 服务使用默认的 wsHttpBinding。无论我在哪里使用 WCF,我都以以下方式使用它:

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

消息中显示的错误行似乎在最后一个 proxy.close 之后。不知道发生了什么。我正在从 Visual Studio 08 中启动该服务。

这是跟踪信息:

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)
4

5 回答 5

67

您应该避免将客户端代理放在using块中。

于 2009-02-10T05:23:06.920 回答
20

更新

这个链接的答案描述了一种使用 C# 语法做同样事情的更干净、更简单的方法。


原帖

这是 Microsoft 推荐的处理 WCF 客户端调用的方法:

有关更多详细信息,请参阅:预期异常

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();
}

附加信息

似乎有很多人在 WCF 上问这个问题,以至于微软甚至创建了一个专门的示例来演示如何处理异常:

c:\WF_WCF_Samples\WCF\Basic\Client\ExpectedExceptions\CS\client

下载示例: C#VB

考虑到涉及到 using 语句的问题很多,(加热?)关于这个问题的内部讨论线程,我不会浪费我的时间尝试成为代码牛仔并找到更清洁的方法。我将把它吸干,并为我的服务器应用程序以这种冗长(但受信任)的方式实现 WCF 客户端。

于 2011-02-19T04:36:36.003 回答
7

如果传输模式为Buffered,则确保MaxReceivedMessageSizeMaxBufferSize的值相同。经过几个小时的努力,我刚刚以这种方式解决了错误状态问题,并认为如果它对某人有帮助,我会在这里发布它。

于 2014-04-24T02:38:45.740 回答
1

此错误也可能是由使用 OperationContract 属性标记的零个方法引起的。这是我在构建新服务并进行长时间测试时遇到的问题。

于 2015-04-21T15:53:34.320 回答
0

与 Ryan Rodemoyer 的回答类似,我发现当合同上的 UriTemplate 无效时,您会收到此错误。就我而言,我两次使用相同的参数。例如:

/Root/{Name}/{Name}
于 2016-04-13T19:07:10.347 回答