PostAsync 调用消失,网页再次可用
PostAsync call vanishes and web page becomes available again
我有一个网页正在尝试调用 Web 服务,但在调用时它跳过了进程。
这就是我指的...
当我在客户输入支票信息后调用此方法时:
public CheckInfo SubmitCheck(CheckInfo checkInfo)
{
try
{
var check = new Check();
check.account_number = checkInfo.CheckAccountNumber;
check.transit_number = checkInfo.CheckRoutingNumber;
check.amount = checkInfo.Amount.ToString();
check.check_number = checkInfo.CheckNumber;
check.bill_to_city = checkInfo.City;
check.bill_to_country = "US";
check.bill_to_postal_code = checkInfo.Zip;
check.bill_to_street = checkInfo.Street;
check.bill_to_state = checkInfo.State;
check.name_on_check = checkInfo.NameOnCheck;
check.transaction_type = "sale";
check.account_type = checkInfo.AccountType;
check.check_type = checkInfo.CheckType;
var ent = new SuburbanPortalEntities();
var gatewaySettings = (from x in ent.GatewayUsers
where x.TokenId == CurrentCustomerSession.Current.TokenId &&
x.Gateway.Name == "DirectAch2"
select x).FirstOrDefault();
var credentials = new Authentication();
credentials.password = gatewaySettings.Password;
credentials.username = gatewaySettings.UserName;
var response = Process.SubmitCheck(credentials, check).Result;
调用私有的publicclassclass:
public static async Task<Response> SubmitCheck(Authentication authentication, Check check)
{
return await Submit(authentication, check, PaymentTypes.Check);
}
提交检查方法:
private static async Task<Response> Submit(Authentication authentication, Object payment, PaymentTypes paymentType)
{
var resp = new Response();
try
{
var client = new HttpClient();
var bodyjson = JsonConvert.SerializeObject(authentication);
var bodycontent = new StringContent(bodyjson, Encoding.UTF8, "application/json");
var authenticationPost =
await client.PostAsync("https://someplace.com/api/v2/Identity", bodycontent);
var bodyResponseJson = await authenticationPost.Content.ReadAsStringAsync();
当我到达这一行时,它只是 returns 超出了方法并且它没有继续任何东西,就像我从未执行过这个方法一样。
var authenticationPost =
await client.PostAsync("https://someplace.com/api/v2/Identity", bodycontent);
此行之后没有执行其他代码。它只是停止,网页再次可用。我的方法包含在 try catch 中,但 catch 没有.. 捕获任何东西。
我对此一头雾水,有什么建议吗?
编辑#1
我按照建议将线包裹在 try catch 中。
try
{
authenticationPost =
await client.PostAsync("https://someplace.com/api/v2/Identity", bodycontent);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
它正在跳出 try/catch,此后不再执行任何代码。没有捕获到异常,什么都没有,它只是运行该行并离开该方法。
这是你的问题:
var response = Process.SubmitCheck(credentials, check).Result;
Don't block on async code,正如我在我的博客中描述的那样。对于 async
/await
的新手来说,这是一个常见的错误。使用 await
:
而不是 Result
public async Task<CheckInfo> SubmitCheck(CheckInfo checkInfo)
{
...
var response = await Process.SubmitCheck(credentials, check);
请注意,您随后需要 await
调用 SubmitCheck
,依此类推。这是 async
all the way.
旁注:我建议在方法名称上使用 standard pattern 或 *Async
后缀;它在代码中更清楚地表明它们的 return 值需要 await
ed:
public async Task<CheckInfo> SubmitCheckAsync(CheckInfo checkInfo)
{
...
var response = await Process.SubmitCheckAsync(credentials, check);
我有一个网页正在尝试调用 Web 服务,但在调用时它跳过了进程。
这就是我指的...
当我在客户输入支票信息后调用此方法时:
public CheckInfo SubmitCheck(CheckInfo checkInfo)
{
try
{
var check = new Check();
check.account_number = checkInfo.CheckAccountNumber;
check.transit_number = checkInfo.CheckRoutingNumber;
check.amount = checkInfo.Amount.ToString();
check.check_number = checkInfo.CheckNumber;
check.bill_to_city = checkInfo.City;
check.bill_to_country = "US";
check.bill_to_postal_code = checkInfo.Zip;
check.bill_to_street = checkInfo.Street;
check.bill_to_state = checkInfo.State;
check.name_on_check = checkInfo.NameOnCheck;
check.transaction_type = "sale";
check.account_type = checkInfo.AccountType;
check.check_type = checkInfo.CheckType;
var ent = new SuburbanPortalEntities();
var gatewaySettings = (from x in ent.GatewayUsers
where x.TokenId == CurrentCustomerSession.Current.TokenId &&
x.Gateway.Name == "DirectAch2"
select x).FirstOrDefault();
var credentials = new Authentication();
credentials.password = gatewaySettings.Password;
credentials.username = gatewaySettings.UserName;
var response = Process.SubmitCheck(credentials, check).Result;
调用私有的publicclassclass:
public static async Task<Response> SubmitCheck(Authentication authentication, Check check)
{
return await Submit(authentication, check, PaymentTypes.Check);
}
提交检查方法:
private static async Task<Response> Submit(Authentication authentication, Object payment, PaymentTypes paymentType)
{
var resp = new Response();
try
{
var client = new HttpClient();
var bodyjson = JsonConvert.SerializeObject(authentication);
var bodycontent = new StringContent(bodyjson, Encoding.UTF8, "application/json");
var authenticationPost =
await client.PostAsync("https://someplace.com/api/v2/Identity", bodycontent);
var bodyResponseJson = await authenticationPost.Content.ReadAsStringAsync();
当我到达这一行时,它只是 returns 超出了方法并且它没有继续任何东西,就像我从未执行过这个方法一样。
var authenticationPost =
await client.PostAsync("https://someplace.com/api/v2/Identity", bodycontent);
此行之后没有执行其他代码。它只是停止,网页再次可用。我的方法包含在 try catch 中,但 catch 没有.. 捕获任何东西。
我对此一头雾水,有什么建议吗?
编辑#1
我按照建议将线包裹在 try catch 中。
try
{
authenticationPost =
await client.PostAsync("https://someplace.com/api/v2/Identity", bodycontent);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
它正在跳出 try/catch,此后不再执行任何代码。没有捕获到异常,什么都没有,它只是运行该行并离开该方法。
这是你的问题:
var response = Process.SubmitCheck(credentials, check).Result;
Don't block on async code,正如我在我的博客中描述的那样。对于 async
/await
的新手来说,这是一个常见的错误。使用 await
:
Result
public async Task<CheckInfo> SubmitCheck(CheckInfo checkInfo)
{
...
var response = await Process.SubmitCheck(credentials, check);
请注意,您随后需要 await
调用 SubmitCheck
,依此类推。这是 async
all the way.
旁注:我建议在方法名称上使用 standard pattern 或 *Async
后缀;它在代码中更清楚地表明它们的 return 值需要 await
ed:
public async Task<CheckInfo> SubmitCheckAsync(CheckInfo checkInfo)
{
...
var response = await Process.SubmitCheckAsync(credentials, check);