HttpClient - task was cancelled - How to get the exact error message?

前端 未结 4 1974
没有蜡笔的小新
没有蜡笔的小新 2021-01-31 01:44

I have the following test code. I always get the \"Task was cancelled\" error after looping 316934 or 361992 times.

If I am not wrong, there are two possible reasons why

相关标签:
4条回答
  • 2021-01-31 02:34

    I ran across this recently. As it turned out, when I started up the web application in debug mode, the start up URL was using https.

    However, the URL for WebApi endpoint (in my config file) was using http. Once I update it to use https, I was able to call the WebApi endpoint.

    0 讨论(0)
  • 2021-01-31 02:40

    just wanted to share I have had a similar code to load test our servers ,and its a high probability that your requests are timing out. You can set the timeout for your http request to max and see if it changes anything for you. I tried hitting our servers by creating various threads. And it increased the hits but they would all eventually timeout.And also you cannot set a timeout when hitting them on another thread.

    0 讨论(0)
  • 2021-01-31 02:43

    The default HttpClient.Timeout value is 100 seconds (00:01:40). If you do a timestamp in your catch block you will notice that tasks begin to get canceled at exactly that time. Apparently there is a limited number of HTTP requests you can do per second, others get queued. Queued requests get canceled on timeout. Out of all 600k of tasks I personally got only 2500 successful, others got canceled.

    I also find it unlikely, that you will be able to run the whole 600000 of tasks. Many network drivers let through high number of requests only for a small time, and reduce that number to a very low value after some time. My network card allowed me to send only 921 requests within 36 seconds and dropped that speed to only one request per second. At that speed it will take a week to complete all the tasks.

    If you are able to bypass that limitation, make sure you build the code for 64-bit platform as the app is very hungry for memory.

    0 讨论(0)
  • 2021-01-31 02:46

    Don't dispose the instance of HttpClient you're using. Weird but fixed for me this problem.

    0 讨论(0)
提交回复
热议问题