0

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.

ErocM
  • 4,505
  • 24
  • 94
  • 161
  • Have you tried adding a breakpoint to both `var authenticationPost` and `var bodyResponseJson`? Also what does your catch look like? – Jerdine Sabio Feb 17 '22 at 15:05
  • Probably there is an exception thrown, that is why the code doesn't continue executing. Look at the logs of the application or wrap this line in a try catch block and see if the catch block is hit. – fbede Feb 17 '22 at 15:28
  • @all ty for the help. I've updated my post. – ErocM Feb 17 '22 at 16:06

1 Answers1

1

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);
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Thank you very much for responding. I am getting a response out of it now but it's not waiting for the operating to complete. I'm getting WaitingForActivation Result = not yet computed. – ErocM Feb 17 '22 at 19:05
  • I see the issue. I'm not waiting for it on the main thread. I'm looking closer at it. – ErocM Feb 17 '22 at 19:08