【问题标题】:WCF communication errorWCF 通信错误
【发布时间】:2018-11-17 20:18:39
【问题描述】:

几天来,我们一直遇到 WCF 通信错误,我无法弄清楚是什么导致了该错误。我最初认为这是一个 SSL 证书问题,但事实并非如此。我还确保只需在 Web 浏览器上输入 URL 即可访问端点并且它是可查看的。我还确保默认请求大小不会导致问题。我确定请求小于默认的 30MB。

还有哪些其他方法可以防止此堆栈跟踪错误?

客户端堆栈跟踪:

由于通信失败,WCFDirector 获取失败。

由于表单未正确加载,因此必须将其关闭。 ---> OSI.Framework.FrameworkException: WCFDirector fetch 由于通信失败而失败。 ---> System.ServiceModel.CommunicationException:底层连接已关闭:预期保持活动状态的连接已被服务器关闭。 ---> System.Net.WebException:底层连接已关闭:预期保持活动状态的连接已被服务器关闭。 ---> System.IO.IOException: Unable to read data from the transport connection: 一个现有的连接被远程主机强行关闭。 ---> System.Net.Sockets.SocketException: 现有连接被远程主机强行关闭 在 System.Net.Sockets.Socket.Receive(Byte[] 缓冲区,Int32 偏移量,Int32 大小,SocketFlags socketFlags) 在 System.Net.Sockets.NetworkStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 大小) --- 内部异常堆栈跟踪结束 --- 在 System.Net.Sockets.NetworkStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 大小) 在 System.Net.FixedSizeReader.ReadPacket(字节 [] 缓冲区,Int32 偏移量,Int32 计数) 在 System.Net.Security._SslStream.StartFrameHeader(字节 [] 缓冲区,Int32 偏移量,Int32 计数,AsyncProtocolRequest asyncRequest) 在 System.Net.Security._SslStream.StartReading(字节 [] 缓冲区,Int32 偏移量,Int32 计数,AsyncProtocolRequest asyncRequest) 在 System.Net.Security._SslStream.ProcessRead(字节 [] 缓冲区,Int32 偏移量,Int32 计数,AsyncProtocolRequest asyncRequest) 在 System.Net.TlsStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 大小) 在 System.Net.PooledStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 大小) 在 System.Net.Connection.SyncRead(HttpWebRequest 请求,布尔 userRetrievedStream,布尔 probeRead) --- 内部异常堆栈跟踪结束 --- 在 System.Net.HttpWebRequest.GetResponse() 在 System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan 超时) --- 内部异常堆栈跟踪结束 ---

服务器堆栈跟踪:

在 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason) 在 System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan 超时) 在 System.ServiceModel.Channels.RequestChannel.Request(消息消息,TimeSpan 超时) 在 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息消息,TimeSpan 超时) 在 System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔单向,ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出,TimeSpan 超时) 在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage 方法调用,ProxyOperationRuntime 操作) 在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)

【问题讨论】:

  • 看起来像 IO 异常,可能需要添加线程。看到我们的服务正在尝试做的代码会很痛苦。
  • 在两边开启WCF追踪,也可以使用Wireshark之​​类的工具来抓取网络数据包。然后查看连接 (SSL/TLS) 是如何关闭的。许多因素都可能导致这种情况,例如代理问题或简单的电汇问题。
  • @LexLi 奇怪的是,如果达到 5 分钟标记,请求总是会失败。服务器或服务 web.config 上可能的超时设置是什么

标签: c# wcf iis wcf-binding wcf-security


【解决方案1】:

尝试关闭 KeepAlive。 比如:

var client = new MyWcfClient();
var customBinding = new CustomBinding(client.Endpoint.Binding);
var transportElement = customBinding.Elements.Find<HttpTransportBindingElement>();

transportElement.KeepAliveEnabled = false;

client.Endpoint.Binding = customBinding;

仅用于 Rest API 和 SOAP 的配置版本

<services>
  <service name="SecureTransportDemo.Service.GreetingService">
    <endpoint address="Soap"
              binding="customBinding" 
              bindingConfiguration="soap-secure-nokeepalive"
              contract="SecureTransportDemo.Service.IGreetingService"
              name="soap-nokeepalive"/>
    <endpoint address="Rest"
              binding="customBinding"
              bindingConfiguration="rest-secure-nokeepalive"
              behaviorConfiguration="web"
              contract="SecureTransportDemo.Service.IGreetingService"
              name="rest-nokeepalive"/>
  </service>
</services>
<bindings>
  <customBinding>
    <binding name="soap-secure-nokeepalive">
      <textMessageEncoding />
      <httpsTransport allowCookies="false" 
                      keepAliveEnabled="false"/>
    </binding>
    <binding name="rest-secure-nokeepalive">
      <webMessageEncoding />
      <httpsTransport manualAddressing="true" 
                      allowCookies="false" 
                      keepAliveEnabled="false"/>
    </binding>
  </customBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp automaticFormatSelectionEnabled="true"
               faultExceptionEnabled="true"
               helpEnabled="true"/>
    </behavior>
  </endpointBehaviors>
</behaviors>

【讨论】:

  • 遗憾的是我无法更改代码。该服务正在为不同服务器上的其他客户端工作。这与服务器的配置方式有关。
  • 这些是您的客户端代理设置,而不是服务设置。因此,您可以在不打扰其他客户的情况下与一位客户一起尝试。
  • 添加了仅配置版本
猜你喜欢
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-17
相关资源
最近更新 更多