. net core webapi using httpclient to send asynchronous request encountered taskcanceledexception: a task was cancelled


Recently, I use more. Net core projects. When using httpclient to send requests recently, when the server takes a long time to process, I always report an exception: a task was cancelled. My method of using asynchronous request is also old-fashioned. I use task a = () = > {}; a.wait(); to wait for the end of the asynchronous operation. When executing the wait in this way, when there is an error in the program, the above exception information will appear. At this time, we need to check the internal exception in the exception to see if there is any useful information?


Common internal exceptions are: a task was cancelled

          The response ended prematurely.

          Unable to read data from the transport connection

          Error while copying content to a stream.




Solution: there are many answers to similar questions on the Internet. My personal task cancellation is due to the error caused by the timeout of visiting the foreign target server, which leads to the asynchronous task cancellation exception. Of course, this is still in the big God’s reminder to guess! It is normal to request foreign server in the over wall environment, wipe it!! In the pit, I thought there was something wrong with the httpclient of. Net core. I don’t know why. Finally, I found out. Encounter similar problems of friends, their own check under the internal exception, more Baidu Bar, if the request is a foreign server, you can try the effect after the wall.