Home > database >  PostAsync call vanishes and web page becomes available again
PostAsync call vanishes and web page becomes available again

Time:02-18

I have a web page that I'm trying to call a web service and I'm having it skip out of the process when called.

Here's what I'm referring to...

When I call this method after the customer enters in their check information:

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;

The public class that calls the private class:

public static async Task<Response> SubmitCheck(Authentication authentication, Check check)
{
  return await Submit(authentication, check, PaymentTypes.Check);
}

The SubmitCheck Method:

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();

When I get to this line, it just returns out of the method and it doesn't continue on with anything, it's like I never executed this method.

    var authenticationPost =
      await client.PostAsync("https://someplace.com/api/v2/Identity", bodycontent);

No other code is executed after this line. It just stops and the web page becomes available again. I have the method wrapped in a try catch but the catch isn't.. catching anything.

I'm at a loss at this poing, any suggestions?

EDIT#1

I wrapped the line in a try catch as suggested.

    try
    {
    authenticationPost =
      await client.PostAsync("https://someplace.com/api/v2/Identity", bodycontent);
    }
    catch (Exception e)
    {
      Console.WriteLine(e);
      throw;
    }

It's stepping out of the try/catch and never executing any more code after that. No exception is caught, nothing, it just runs the line and leaves the method.

CodePudding user response:

Here's your problem:

var response = Process.SubmitCheck(credentials, check).Result;

Don't block on async code, as I describe on my blog. This is a common mistake for those new to async/await. Instead of Result, use await:

public async Task<CheckInfo> SubmitCheck(CheckInfo checkInfo)
{
  ...
  var response = await Process.SubmitCheck(credentials, check);

Note that you'd then need to await the call to SubmitCheck, and so on. It's async all the way.

Side note: I recommend using the standard pattern of *Async suffixes on your method names; it makes it clearer in the code that their return values need to be awaited:

public async Task<CheckInfo> SubmitCheckAsync(CheckInfo checkInfo)
{
  ...
  var response = await Process.SubmitCheckAsync(credentials, check);
  • Related