且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HttpResponseMessage"不包含"GetAwaiter"的定义,也没有可访问的扩展方法"GetAwaiter"

更新时间:2021-12-02 21:30:26

不要调用 Result ,当使用 async and await模式时,可能会在***的情况下引起问题

 var apiResponse = await apiClient.GetAsync($"http://xxx/weatherforecast");

但是,问题是因为您试图使用与 await 关键字相关联的语言功能,而该关键字需要 awaitable .编译器要求 await 必须满足某些约束. awaitable 必须实现 GetAwaiter 方法, INotifyCompletion IsCompleted GetResult 方法.错误消息描述的是什么.

However, the problem is because you are trying to use a language feature associated with the await keywords that requires an awaitable. The compiler dictates that to await something it must satisfy certain constraints. An awaitable must implement the GetAwaiter method, INotifyCompletion, IsCompleted and GetResult method. Which is what the error message is describing.

这是由于您调用了 Result< T> 方法,该方法返回了 task 的结果值(在这种情况下,返回了Task的结果)来自 GetAsync< Task< T>> ,即您的 HttpResponseMessage ).然后,您尝试对其进行 await ,就像它不是 task / awaitable 一样.

This is due to the fact you have called the Result<T> method which returns the result value of a task (in this case the result from the Task returned from GetAsync<Task<T>>, your HttpResponseMessage). You are then trying to await it like it is a task / awaitable, which it is not.

通常,在现代时代很少有调用 Result Wait 实际上是个好主意的情况.

In general, there are very few cases in the modern-era where calling Result, or Wait is actually a good idea.