当我尝试使用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会抛出异常?
答案 0 :(得分:3)
事实证明,不支持get-only属性作为数据成员。连接被切断的原因仍然是个谜。通常,WCF会返回错误文档。
WCF: Exposing readonly DataMember properties without set?您可以添加一个投注NotSupportedException
。