Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么wait不等到httpclient postasync完成?_C#_Async Await_Xmlhttprequest_Httpclient_Httpresponse - Fatal编程技术网

C# 为什么wait不等到httpclient postasync完成?

C# 为什么wait不等到httpclient postasync完成?,c#,async-await,xmlhttprequest,httpclient,httpresponse,C#,Async Await,Xmlhttprequest,Httpclient,Httpresponse,我正在为一些api创建http客户端 下面是我调用api端点的客户端方法 public async Task<HttpResponseMessage> SendRequestAsync() { string adaptiveUri = "https://some-api/api/Authentication/AuthenticateThirdPartyUserAsync"; using (HttpClient htt

我正在为一些api创建http客户端

下面是我调用api端点的客户端方法

public async Task<HttpResponseMessage> SendRequestAsync()
        {

            string adaptiveUri = "https://some-api/api/Authentication/AuthenticateThirdPartyUserAsync";

            using (HttpClient httpClient = new HttpClient())
            {
                var json = JsonConvert.SerializeObject(new { userName = "uname", password = "123", applicantCode = "hello" });
                var payload = new StringContent(json, Encoding.UTF8, "application/json");

                HttpResponseMessage responseMessage = null;
                try
                {
                    responseMessage = await httpClient.PostAsync(adaptiveUri, payload);
                }
                catch (Exception ex)
                {
                    if (responseMessage == null)
                    {
                        responseMessage = new HttpResponseMessage();
                    }
                    responseMessage.StatusCode = HttpStatusCode.InternalServerError;
                    responseMessage.ReasonPhrase = string.Format("RestHttpClient.SendRequest failed: {0}", ex);
                }
                return responseMessage;
            }
        }
它等待post完成

有人知道这件事吗


谢谢。

等待_iAClient.SendRequestAsync()肯定会等待
PostAsync

我认为问题在于您在
SendRequestAsync
中接收到异常,并混淆了结果

完全删除
try/carch

public async Task<HttpResponseMessage> SendRequestAsync()
{
    string adaptiveUri ="https://someapi/api/Authentication/AuthenticateThirdPartyUserAsync";

    using (HttpClient httpClient = new HttpClient())
    {
        var json = JsonConvert.SerializeObject(new { userName = "uname", password = "123", applicantCode = "hello" });
        var payload = new StringContent(json, Encoding.UTF8, "application/json");
        return await httpClient.PostAsync(adaptiveUri, payload);                
    }
}
公共异步任务SendRequestAsync()
{
字符串自适应URI=”https://someapi/api/Authentication/AuthenticateThirdPartyUserAsync";
使用(HttpClient HttpClient=new HttpClient())
{
var json=JsonConvert.SerializeObject(新的{userName=“uname”,password=“123”,applicationcode=“hello”});
var payload=newstringcontent(json,Encoding.UTF8,“application/json”);
返回wait wait httpClient.PostAsync(自适应URI,有效负载);
}
}
并尝试在调用方中捕获它:

public async Task<IBaseStatus> Handle(InspectionAddedEvent domainEvent)
{
    try 
    {
        var tk = await _iAClient.SendRequestAsync();
        return something;
    }
    catch(Exception ex)
    {
        //Probably return some IBaseStatus
    }       
}
公共异步任务句柄(InspectionAddedEvent domainEvent)
{
尝试
{
var tk=await_iAClient.SendRequestAsync();
归还某物;
}
捕获(例外情况除外)
{
//可能还有些IBaseStatus
}       
}

不等到完成是什么意思?您如何知道它不完整?请查看发布代码指南和问题,以显示再现错误行为所需的所有代码-到目前为止,发布中显示的代码是进行异步调用的标准代码,没有理由相信它会按照您描述的方式运行。您可能希望完全删除所有异常处理(除非是同步路径所在的位置,即无效的DNS名称可能会使此代码同步失败)。没有机会,im从静态类调用是否需要等待效果?
public async Task<HttpResponseMessage> SendRequestAsync()
{
    string adaptiveUri ="https://someapi/api/Authentication/AuthenticateThirdPartyUserAsync";

    using (HttpClient httpClient = new HttpClient())
    {
        var json = JsonConvert.SerializeObject(new { userName = "uname", password = "123", applicantCode = "hello" });
        var payload = new StringContent(json, Encoding.UTF8, "application/json");
        return await httpClient.PostAsync(adaptiveUri, payload);                
    }
}
public async Task<IBaseStatus> Handle(InspectionAddedEvent domainEvent)
{
    try 
    {
        var tk = await _iAClient.SendRequestAsync();
        return something;
    }
    catch(Exception ex)
    {
        //Probably return some IBaseStatus
    }       
}