[英]Call async method HttpClient without await throw error
當我在沒有等待的情況下使用 httpclient 調用異步方法時,我不想等待響應然后拋出異常“對象引用未設置為對象的實例”
\\ Call PostAsync
var ignore = webRequest.PostAsync(json, token);
\\ PostAsync method
using (var httpClient = new HttpClient())
{
if (!string.IsNullOrEmpty(auth))
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", auth);
var content = new StringContent(json, Encoding.UTF8, "application/json");
// this line throw exception
var response = await httpClient.PostAsync(this.URL, content);
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync();
}
}
然后我在下面的視覺工作室圖像上出現錯誤。
如果我不想等待回復,我該怎么做?
我認為問題是異步等待的基本概念,請查看此鏈接以獲得更清晰的想法:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.