c# - WCF 超时太快 & 'Connection was closed unexpectedly' 异常

标签 c# .net wcf exception iis

当调用返回字符串“成功”的方法时,WCF 客户端在 120 秒后返回上述异常。来自服务器的日志显示它完全执行并在大约 3 分钟内完成。启用跟踪后,我没有看到任何错误抛出。 任何帮助将不胜感激,因为不幸的是,来自类似问题的其他建议没有帮助。谢谢!

编辑 - 已解决:原来客户端使用的是 Smoothwall 过滤设备,该设备在两分钟后关闭连接。

异常(exception):

System.ServiceModel.CommunicationException The underlying connection was closed: The connection was closed unexpectedly.
Stack Trace:    at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

内部异常:

System.Net.WebException The underlying connection was closed: The connection was closed unexpectedly.The underlying connection was closed: The connection was closed unexpectedly.
Stack Trace:    at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

据我所知,绑定(bind)设置正确且超时超过 120 秒。这是客户端绑定(bind):

<bindings>
  <customBinding>
      <binding name="CustomBinding_IPHG" closeTimeout="00:05:00"
          openTimeout="00:05:00" receiveTimeout="00:15:00" sendTimeout="00:15:00">
          <security authenticationMode="SecureConversation" requireDerivedKeys="false">
              <localClientSettings maxClockSkew="00:30:00" />
              <localServiceSettings maxClockSkew="00:30:00" />
              <secureConversationBootstrap authenticationMode="UserNameOverTransport">
                  <localClientSettings maxClockSkew="00:30:00" />
                  <localServiceSettings maxClockSkew="00:30:00" />
              </secureConversationBootstrap>
          </security>
          <textMessageEncoding>
              <readerQuotas maxStringContentLength="5242880" />
          </textMessageEncoding>
          <httpsTransport maxReceivedMessageSize="6553600" useDefaultWebProxy="true" />
      </binding>
  </customBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="commonBehaviour">
  <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
  <clientCredentials>
    <serviceCertificate>
      <authentication certificateValidationMode="PeerOrChainTrust"/>
    </serviceCertificate>
  </clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>

这是服务器绑定(bind):

<bindings>
  <customBinding>
    <binding name="CommonBinding" closeTimeout="00:05:00" openTimeout="00:05:00" receiveTimeout="00:15:00" sendTimeout="00:15:00">
      <transactionFlow />
      <security authenticationMode="SecureConversation" requireDerivedKeys="false">
        <secureConversationBootstrap authenticationMode="UserNameOverTransport">
          <localServiceSettings maxClockSkew="00:30:00" />
          <localClientSettings maxClockSkew="00:30:00" />
        </secureConversationBootstrap>
        <localServiceSettings maxClockSkew="00:30:00" />
        <localClientSettings maxClockSkew="00:30:00" />
      </security>
      <textMessageEncoding>
        <readerQuotas maxStringContentLength="41943040"/>
      </textMessageEncoding>
      <httpsTransport maxBufferSize="41943040" maxReceivedMessageSize="41943040" />
    </binding>
  </customBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="BehaviorRBS">
      <serviceMetadata httpsGetEnabled="true" httpGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="false" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="PHGAPI.PHGUserNamePassValidator, PHGAPI"/>
        <serviceCertificate
                findValue="*.domainname.com"
                x509FindType="FindBySubjectName"
                storeLocation="LocalMachine"
                storeName="My" />
      </serviceCredentials>
      <serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="400" maxConcurrentInstances="600" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<services>
    <service behaviorConfiguration="BehaviorRBS" name="PHGAPI.iAPI">
      <endpoint address="" binding="customBinding" bindingConfiguration="CommonBinding" contract="PHGAPI.IiAPI" />
    </service>
</services>

这是相关方法的接口(interface):

[OperationContract]
string SetSuspensions(List<Suspension> suspensions);

最佳答案

我会查看默认值为 120 秒的 WCF 和相关配置设置。为此,您可能需要查看 HTTP.sys connectionTimeout 值。

来自微软文档:
由于以下原因,连接可以被视为不活动:

  • HTTP.sys Timer_ConnectionIdle 计时器已过期。连接 已过期并保持闲置状态。
  • HTTP.sys Timer_EntityBody 计时器已过期。连接已过期 在请求实体主体到达之前。当很明显 请求有一个实体主体,HTTP API 打开 Timer_EntityBody 定时器。最初,此计时器的限制设置为 连接超时值。每次另一个数据指示是 收到此请求后,HTTP API 会重置计时器以提供 connectionTimeout 中指定的连接更多分钟 属性。
  • HTTP.sys Timer_AppPool 计时器已过期。连接已过期 因为请求在应用程序池队列中等待太久 服务器应用程序出列并处理它。本次超时时间 是连接超时。

默认值为 00:02:00(两分钟)。

http://www.iis.net/configreference/system.applicationhost/sites/sitedefaults/limits

祝你好运。

关于c# - WCF 超时太快 & 'Connection was closed unexpectedly' 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22507779/

相关文章:

c# - 当我的任何 UI 元素在 WPF/C# 中发生更改时,是否会触发任何事件?

.net - 如何避免 File.Exists/File.Delete 或 Directory.Exists/Directory.Delete 中的竞争

c# - 阅读 .Net 堆栈跟踪

c# - 使用 .net 打开 Google 电子表格

android - WCF 对象数组参数的 JSON 请求

wcf - 如何使用远程计算机上托管的 wcf 服务?

生产中的 WCF "Too many active security negotiations"错误

c# - LINQ 方法的序列有什么重要性吗?

c# - 违反 ASP.NET Core 中授权属性的默认行为是什么

c# - 将表类型从 C# 传递到存储过程?