简体   繁体   English

在没有等待抛出错误的情况下调用异步方法 HttpClient

[英]Call async method HttpClient without await throw error

When i call async method using httpclient without await cause i don't want to wait response then throw exception "object reference not set to an instance of an object"当我在没有等待的情况下使用 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();
    }                    
}

and then i get error on my visual studio image below.然后我在下面的视觉工作室图像上出现错误。

image图片

if i don't want to wait response, how i do it?如果我不想等待回复,我该怎么做?

I think the problem is the basic concept of async await, check this link for a clearer idea:我认为问题是异步等待的基本概念,请查看此链接以获得更清晰的想法:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/ https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM