如何等待 xamarin 中的 PostAsync 方法?
How to await for an PostAsync method in xamarin?
我正在尝试在 xamarin 项目中做类似 await client.PostAsync()
的事情,但它不会等待。我怎样才能让它等待? winforms 项目中的相同代码会等待。
我的代码
private bool ConfirmLogin(string user, string password)
{
bool result = false;
AppLoginRequest appLoginRequest = new AppLoginRequest();
AppLoginResponse appLoginResponse = new AppLoginResponse();
// step 1
Task<HttpResponseMessage> task = GetAppLogin(appLoginRequest, appLoginResponse);
// step 4
result = appLoginResponse.Authorized;
return result;
}
以及 GetAppLogin 的代码
private async Task<HttpResponseMessage> GetAppLogin(AppLoginRequest appLoginRequest, AppLoginResponse appLoginResponse)
{
HttpResponseMessage response = null;
string JsonData = JsonConvert.SerializeObject(appLoginRequest);
System.Net.Http.StringContent restContent = new StringContent(JsonData, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
try
{
// step 2
response = await client.PostAsync(@"http://x.x.x.x:xxxx/api/XXX/GetAppLogin", restContent);
// step 3
if (response.IsSuccessStatusCode)
{
var stream = await response.Content.ReadAsStringAsync();
AppLoginResponse Result = JsonConvert.DeserializeObject<AppLoginResponse>(stream);
}
else
{
appLoginResponse.Remark = response.ReasonPhrase;
}
}
catch (Exception ex)
{
appLoginResponse.Authorized = false;
appLoginResponse.Remark = ex.Message;
}
return response;
}
我需要的是以正确的顺序执行步骤(//代码注释中的步骤 x)。
Step 1, then step 2, then step 3 and finally step 4
但是它是这样执行的
step 1 then step 2 and then step 4 and step 3
这当然会打乱完整的逻辑,有什么办法可以强制 client.PostAsync
真正等待吗?
我发现了数百个关于如何同步 运行 异步任务的问题,但似乎都不起作用。
xamarin 在这方面有什么不同吗?
我 运行 来自用 winforms 编写的测试客户端的完全相同的代码,它确实在等待。
async void ButtonClick(...)
{
bool x = await ConfirmLogin(..., ...);
}
private async Task<bool> ConfirmLogin(string user, string password)
{
bool result = false;
AppLoginRequest appLoginRequest = new AppLoginRequest();
AppLoginResponse appLoginResponse = new AppLoginResponse();
// step 1
await GetAppLogin(appLoginRequest, appLoginResponse);
// step 4
result = appLoginResponse.Authorized;
return result;
}
可以避免时不要使用async void
。这是一个专门为事件处理程序设计的逃避方法。 “让异步运行”
我正在尝试在 xamarin 项目中做类似 await client.PostAsync()
的事情,但它不会等待。我怎样才能让它等待? winforms 项目中的相同代码会等待。
我的代码
private bool ConfirmLogin(string user, string password)
{
bool result = false;
AppLoginRequest appLoginRequest = new AppLoginRequest();
AppLoginResponse appLoginResponse = new AppLoginResponse();
// step 1
Task<HttpResponseMessage> task = GetAppLogin(appLoginRequest, appLoginResponse);
// step 4
result = appLoginResponse.Authorized;
return result;
}
以及 GetAppLogin 的代码
private async Task<HttpResponseMessage> GetAppLogin(AppLoginRequest appLoginRequest, AppLoginResponse appLoginResponse)
{
HttpResponseMessage response = null;
string JsonData = JsonConvert.SerializeObject(appLoginRequest);
System.Net.Http.StringContent restContent = new StringContent(JsonData, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
try
{
// step 2
response = await client.PostAsync(@"http://x.x.x.x:xxxx/api/XXX/GetAppLogin", restContent);
// step 3
if (response.IsSuccessStatusCode)
{
var stream = await response.Content.ReadAsStringAsync();
AppLoginResponse Result = JsonConvert.DeserializeObject<AppLoginResponse>(stream);
}
else
{
appLoginResponse.Remark = response.ReasonPhrase;
}
}
catch (Exception ex)
{
appLoginResponse.Authorized = false;
appLoginResponse.Remark = ex.Message;
}
return response;
}
我需要的是以正确的顺序执行步骤(//代码注释中的步骤 x)。
Step 1, then step 2, then step 3 and finally step 4
但是它是这样执行的
step 1 then step 2 and then step 4 and step 3
这当然会打乱完整的逻辑,有什么办法可以强制 client.PostAsync
真正等待吗?
我发现了数百个关于如何同步 运行 异步任务的问题,但似乎都不起作用。
xamarin 在这方面有什么不同吗?
我 运行 来自用 winforms 编写的测试客户端的完全相同的代码,它确实在等待。
async void ButtonClick(...)
{
bool x = await ConfirmLogin(..., ...);
}
private async Task<bool> ConfirmLogin(string user, string password)
{
bool result = false;
AppLoginRequest appLoginRequest = new AppLoginRequest();
AppLoginResponse appLoginResponse = new AppLoginResponse();
// step 1
await GetAppLogin(appLoginRequest, appLoginResponse);
// step 4
result = appLoginResponse.Authorized;
return result;
}
可以避免时不要使用async void
。这是一个专门为事件处理程序设计的逃避方法。 “让异步运行”