FoxPro and .NET Interop
Attempting to use result on async task synchronously
Gravatar is a globally recognized avatar based on your email address. Attempting to use result on async task synchronously
  Naomi
  All
  Oct 4, 2018 @ 12:20pm

Hi Rick,

Remember my problems with Newtonsoft.Json.dll? Ok, this problem is now resolved, but unfortunately I ran into a different issue now and wondering if I should re-write my code in .NET dll.

When I'm testing my dll through our C++/VFP dll/wwDotNetBridge.dll combination, it works OK for me (returning the expected results). Unfortunately, it doesn't work for my colleague and he seems to be getting the same problem as described here https://stackoverflow.com/questions/26343940/one-or-more-errors-occurred-postasjsonasync

I'm wondering if I need to change the dll code or the way I'm calling that result from VFP.

My dll code is the following:

private static async Task<ValidateAccessCodeResponse> GetIkonValidationResultAsync(ValidateAccessCodeRequest request)
        {
            var authorizationKey = Convert.ToBase64String(
             Encoding.ASCII.GetBytes(
                 string.Format("{0}:{1}", cUserName, cPassword)));

            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(cHost);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));

                if (!string.IsNullOrEmpty(cUserName))
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                                   authorizationKey);
                }
                

                var ikonValidationRequest = request;
                HttpResponseMessage response = client.PostAsJsonAsync<ValidateAccessCodeRequest>(url, ikonValidationRequest).Result;

                try
                {
                    var data = await response.Content.ReadAsStringAsync();

                    return JsonConvert.DeserializeObject<ValidateAccessCodeResponse>(data);
                }
                catch (Exception e)
                {
                    //Console.WriteLine(string.Format("error ocurred: {0} stack trace: {1}", e.Message, e.StackTrace));
                    return new ValidateAccessCodeResponse { PassValidityDescription = "Unable to reach endpoint: " + e.Message };
                }
            }
        }

Looks like I need to at least adjust that code in accordance to that thread I found, right?

Thanks in advance.

Gravatar is a globally recognized avatar based on your email address. re: Attempting to use result on async task synchronously
  Rick Strahl
  Naomi
  Oct 4, 2018 @ 12:54pm

What are you calling from Fox exactly? That method?

What error is your colleague getting? I suspect the request is hanging (probably due to some network configuration issues or proxy or firewall) and it just times out for a long time and waits. It probably returns eventually and then throws an exception.

That's the expected behavior actually in error scenarios - you're basically making the async code synchronous by calling .Result so it behaves just like a synchronous call would. Also make sure you're using HttpClient correctly - recommended way is to use a single static instance and reuse it.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Attempting to use result on async task synchronously
  Naomi
  Rick Strahl
  Oct 4, 2018 @ 01:03pm

He is getting 'One or more error occurred error'. There is actually another method I'm calling which calls this one. I'm going for now to adjust the code the way it was recommended in that Stackoverflow thread. My code calling this one is

 while (nTries <= tnRetries)
                {
                    try
                    {
                        var response = GetIkonValidationResultAsync(requestJson);
                        if (response != null && response.Result != null)
                        {
                            //Console.WriteLine(string.Format("Response pass validity description:{0}", response.Result.PassValidityDescription));
                            var result = response.Result;
                            tcAgeBin = result.AgeBin;

So, I'm not using static object for the HttpClient.

Gravatar is a globally recognized avatar based on your email address. re: Attempting to use result on async task synchronously
  Naomi
  Rick Strahl
  Oct 4, 2018 @ 01:04pm

So, this is not recommended, right?

using (HttpClient client = new HttpClient())

Gravatar is a globally recognized avatar based on your email address. re: Attempting to use result on async task synchronously
  Rick Strahl
  Naomi
  Oct 4, 2018 @ 01:29pm

It's been a while since I looked at this but there is a blog post here:

And a help topic here:

As to HttpClient - check for HttpClient best practices and you should find several articles that explain best use cases.

If you're doing this from FoxPro make sure you don't have many requests going at once as that can and likely will cause a deadlock with the Task api due to the .Result blocking. If you want better async support with wwDotnetBridge use the async method calling approach instead.

Gravatar is a globally recognized avatar based on your email address. re: Attempting to use result on async task synchronously
  Naomi
  Rick Strahl
  Oct 5, 2018 @ 02:17pm

Hi Rick,

That has been quite helpful. I changed my code that way (which made no difference to that tester), then I completely re-wrote it again using code very similar to the other dll (different URL, of course).

The dll works fine for me but returns an error for him (slightly different one now).

But the strange thing is that when I try to go to the URL directly (the host), I'm getting an error too (This site can’t be reached redisresort2.centralus.cloudapp.asure.com’s server IP address could not be found. Search Google for redisresort2 centralus cloudapp asure ERR_NAME_NOT_RESOLVED).

Is it possible that somehow the URL is no longer in existence and that myself and a few of my colleagues somehow are getting the cached response?

We did try running tests (yesterday) with him from VS directly and they both worked for him from VS.

© 1996-2024