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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3900040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:48:07  来源:igfitidea点击:

System.Net.WebException: The request was aborted: the request was cancelled

.netwcfsystem.net.webexceptionservice-referencecommunicationexception

提问by Audzzy

I have a WCF service that has been giving me this error under load conditions (and I can't seem to recreate the error otherwise). We've been trying to find a way around it for about a week now with no such luck..

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

The error I see has two parts to it,

我看到的错误有两部分,

System.ServiceModel.CommunicationException: An error: (The request was aborted: the request was cancelled.) occurred while transmitting data over the http channel.

System.ServiceModel.CommunicationException: An error: (The request was aborted: the request was cancelled.) 在通过 http 通道传输数据时发生。

and:

和:

System.Net.WebException: The request was aborted: the request was cancelled.

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

I've seen many people suggest to disable working with keep alive by overloading a method in the Reference.csfile and setting KeepAlive = false, however, our client side is using a service reference (in addition to web reference) and this option does not exist anymore.

我看到很多人建议通过重载Reference.cs文件和设置中的方法来禁用保持活动状态KeepAlive = false,但是,我们的客户端正在使用服务引用(除了 Web 引用)并且此选项不再存在。

Another option I've seen was to add a custom Binding to the service instead of the BasicHttpBindingwe are using now, but that would bother backwards support of the webservice to those who have been using a webReference (since CustomBindingis not SOAP enabled).

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

Has anyone dealt with this error before? Is there a way to disable keep alive in WCF without affecting the server side? Is there anything other that keep alive that is known to cause this error?

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

回答by Ladislav Mrnka

I don't think that HTTP keep alive is responsible for this. WCF should be able to handle this by itself so the HTTP persistant connection is shared among requests and if it expires (it expires after 100s of inactivity) WCF creates new one without firing any exception. If your connection is aborted during request transmission then I expect there will be some other problem.

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

You can use this custom binding as equivalent to BasicHttpBinding without HTTP keep alive:

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

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

回答by nuander

I had this problem when trying to upload large files. I had to add this to the web.config of the web services

我在尝试上传大文件时遇到了这个问题。我不得不将它添加到 web 服务的 web.config

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

回答by C Johnson

I had this exact same problem. In my case I was executing requests ASynchronously. I was sending a few hundred requests to the 'server' from my client. I am/was using basicHttpBinding. And in my app.config setting the openTimeout property was set to 60 seconds or one minute. Once I set that to a bigger number like 10 minutes, the problem has gone away.

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

So for instance I changed all these values in my app.config file:

例如,我在 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"

to 10 minutes.

到 10 分钟。

回答by Anketam

This error can also be caused by mixing usingclauses with asynchronous calls to WCF services.

此错误也可能是由using子句与对 WCF 服务的异步调用混合引起的。

For example:

例如:

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

This will trigger a race condition between how fast can it dispose of proxyversus how fast the asynchronous Task<ResultSet>can be completed. Longer the task takes the more likely it will be to end up in a Faulted state and the Result containing a System.ServiceModel.CommunicationException.

这将触发处理proxy速度与异步Task<ResultSet>完成速度之间的竞争条件。任务花费的时间越长,最终处于故障状态和包含 System.ServiceModel.CommunicationException 的结果的可能性就越大。

This can be fixed by removing the using clause:

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

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

Note that proxyshould be persisted too so that once the asynchronous call is completed that a proxy.Close()can be done.

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