【问题标题】:System.Net.WebException: The request was aborted: the request was cancelledSystem.Net.WebException:请求被中止:请求被取消
【发布时间】:2011-04-23 10:30:12
【问题描述】:

我有一个 WCF 服务,它在负载条件下一直给我这个错误(否则我似乎无法重新创建错误)。我们一直在努力寻找解决它大约一周的方法,但没有这样的运气..

我看到的错误有两个部分,

System.ServiceModel.CommunicationException: 错误:(请求被中止: 请求被取消。)发生 通过 http 传输数据时 频道。

和:

System.Net.WebException:请求 被中止:请求是 取消。

我看到很多人建议通过重载Reference.cs 文件中的方法并设置KeepAlive = false 来禁用keep alive 工作,但是,我们的客户端正在使用服务引用(除了Web 引用)和这个选项已经不存在了。

我看到的另一个选项是向服务添加自定义绑定,而不是我们现在使用的 BasicHttpBinding,但这会妨碍那些一直使用 webReference 的人对 web 服务的向后支持(因为 @987654325 @ 未启用 SOAP)。

以前有人处理过这个错误吗?有没有办法在不影响服务器端的情况下禁用 WCF 中的保持活动?是否还有其他已知会导致此错误的保持活动状态?

【问题讨论】:

标签: .net wcf system.net.webexception service-reference communicationexception


【解决方案1】:

我不认为 HTTP 保持活动对此负责。 WCF 应该能够自行处理此问题,因此 HTTP 持久连接在请求之间共享,并且如果它过期(它在 100s of inactivity 之后过期)WCF 会创建新连接而不会引发任何异常。如果您的连接在请求传输期间中止,那么我预计还会有其他问题。

您可以将此自定义绑定用作等效于 BasicHttpBinding 而无需 HTTP 保持活动:

<bindings>
  <customBinding>
    <binding name="NoKeepAlive">
      <textMessageEncoding messageVersion="Soap11" />
      <httpTransport keepAliveEnabled="false" />
    </binding>
  </customBinding>
</bindings> 

【讨论】:

  • 是的,是负责的,我以前见过。
  • 哇,我自己也经历过。真是个惊喜。谢谢,@Aliostad
  • @Aliostad 出于好奇,您是否还调用了基于 Tomcat 的 Java Web 服务?
【解决方案2】:

我在尝试上传大文件时遇到了这个问题。我必须将此添加到 Web 服务的 web.config 中

<system.web>
  <httpRuntime maxRequestLength="10240" />

【讨论】:

    【解决方案3】:

    我遇到了同样的问题。 就我而言,我正在异步执行请求。我从我的客户端向“服务器”发送了几百个请求。我正在/正在使用 basicHttpBinding。在我的 app.config 设置中,openTimeout 属性设置为 60 秒或一分钟。一旦我将其设置为更大的数字(例如 10 分钟),问题就消失了。

    例如,我在 app.config 文件中更改了所有这些值:

    <configuration>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="BasicHttpBinding_IScriptRunHost" closeTimeout="00:10:00"
                        openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
    

    到 10 分钟。

    【讨论】:

      【解决方案4】:

      在 app.config 中添加这个

       <system.net>
       <connectionManagement>
            <add address="*" maxconnection="5"/>
                      <add address="https://api.limitedconnections.com*" maxconnection="2"/>                
                      <add address="https://api.moreconnections.com*" maxconnection = "10"/>
              </connectionManagement>
      </system.net>
      

      参考以下链接

      https://cdijkgraaf.wordpress.com/2019/12/02/configuring-maxconnection-in-biztalk/

      【讨论】:

        【解决方案5】:

        此错误也可能是由于将 using 子句与对 WCF 服务的异步调用混合使用造成的。

        例如:

        using (ServiceClient proxy = new ServiceClient(proxyName)) {
          proxy.Open();
          return proxy.FunctionCallAsync(parameters); //Return type being Task<ResultSet>
        }
        

        这将触发处理proxy 的速度与异步Task&lt;ResultSet&gt; 的完成速度之间的竞争条件。任务花费的时间越长,它就越有可能最终处于故障状态并且结果包含 System.ServiceModel.CommunicationException。

        这可以通过删除 using 子句来解决:

        ServiceClient proxy = new ServiceClient(proxyName))
        proxy.Open();
        return proxy.FunctionCallAsync(parameters); //Return type being Task<ResultSet>
        

        请注意,proxy 也应该被持久化,这样一旦异步调用完成,proxy.Close() 就可以完成。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-01-28
          • 1970-01-01
          • 1970-01-01
          • 2013-06-18
          • 2022-10-24
          • 2018-10-21
          相关资源
          最近更新 更多