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

前端 未结 5 492
余生分开走
余生分开走 2021-02-04 06:45

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

5条回答
  •  渐次进展
    2021-02-04 07:40

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

    For example:

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

    This will trigger a race condition between how fast can it dispose of proxy versus how fast the asynchronous Task 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.

    This can be fixed by removing the using clause:

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

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

提交回复
热议问题