【发布时间】:2014-07-24 23:01:21
【问题描述】:
当我尝试使用 WCF 测试客户端调用我的 WCF 方法时,我收到以下错误消息:
Failed to invoke the service. Possible causes: The service is offline or
inaccessible; the client-side configuration does not match the proxy; the existing
proxy is invalid. Refer to the stack trace for more detail. You can try to recover
starting a new proxy, restoring to default configuration, or refreshing the
service.
The underlying connection was closed: The connection was closed unexpectedly.
Server stack trace:
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
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 MyApi.GetStuff(String[] stuff)
at IMyApi.GetStuff(String[] stuff)
Inner Exception:
The underlying connection was closed: The connection was closed unexpectedly.
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
我正在测试我的方法,所以我知道异常不是从方法内部抛出的。我还有一个拦截器Interceptor: Attribute, IOperationBehavior, IParameterInspector,其AfterCall 方法报告已到达拦截器。从异常消息、单元测试和拦截器中可以清楚地看出,在调用方法之后,当服务尝试(但失败)发送响应时,以某种方式抛出了异常。
我正在使用 Visual Studio 对 WCF 的内置支持。我没有做任何事情把它放在 IIS 上。 API 中的其他方法似乎工作得很好——只是那个方法不行。出了什么问题?
编辑:经过一些调试,我发现以下代码重现了我的问题。
[ServiceContract]
public class TestService
{
[OperationContract]
public MyClass DoWork()
{
return new MyClass()
{
Str = "hello"
};
}
}
[DataContract]
public class MyClass
{
[DataMember]
public string Str { get; set; }
[DataMember]
public string StrPlus
{
get { return Str + " again!"; }
}
}
添加 StrPlus 怎么会抛出异常?
【问题讨论】:
-
使用 Fiddler 检查失败的请求。检查请求和响应。
-
没有响应。当我使用 SOAP UI 时,它显示“获取响应时出错...目标服务器无法响应。”
-
将服务方法的主体替换为空。通话还是失败吗?
-
没有。我创建了服务方法主体
return null;,调用返回null,正如预期的那样。我确实对返回值做了一些奇怪的事情;我目前正在尝试用一个较小的例子来重现这个问题(地雷对于 Stack Overflow 来说太大了);我的主要怀疑是返回类型无法在 WCF 中序列化。很快就会报告。 -
从光明的一面看它,并将其视为调试中的经验教训。将调试器附加到服务器并将其设置为在所有异常时中断。也许禁用“仅我的代码”以查看 WCF 内部异常。