gpt4 book ai didi

c# - WCF 服务不发送 HTTP 响应,即使方法被调用

转载 作者:行者123 更新时间:2023-11-30 15:28:47 25 4
gpt4 key购买 nike

当我尝试使用 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, IParameterInspectorAfterCall 方法报告已到达拦截器。从异常消息、单元测试和拦截器中可以清楚地看出,当服务尝试(但失败)发送响应时,在调用方法后以某种方式抛出异常。

我正在使用 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 会抛出异常?

最佳答案

事实证明,只获取属性不支持作为数据成员。为什么连接被切断仍然是个谜。通常 WCF 会返回一个错误文档。

WCF: Exposing readonly DataMember properties without set?您可以添加一个 setter 抛出 NotSupportedException

关于c# - WCF 服务不发送 HTTP 响应,即使方法被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24945390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com