0

I'm calling a method to get and parse a JSON from a URL. The code structure is as follows:

internal static void CheckIfSettingsExists()
{
    settingsContainer.Values["ApplicationXCurrentVersion"] = Controller.GetCurrentVersionAsync();
}

and the GetCurrentVersionAsync() function:

internal static async Task<string> GetCurrentVersionAsync()
{
    string temp = null;

    using HttpClient client = new HttpClient();

    try
    {
        temp = await client.GetStringAsync("https://someurl.cloud.io/api/v4/VersionCheck");
    }
    catch (Exception e)
    {
        App.log.Error("Error fetching version check json from API. Exception raised was: " + e.ToString());
        return temp;
    }
    App.log.Info("Successfully got return for VersionCheck");

    if (temp == null)
    {
        return temp;
    }

    dynamic JsonObj = JsonConvert.DeserializeObject<dynamic>(temp);

    return JsonObj["LatestVersion"].ToString();
}

When I was trying to debug what's happening, I saw that after program runs the line temp = await client.GetStringAsync("https://someurl.cloud.io/api/v4/VersionCheck"); instead of executing the catch block, System.ArgumentException: 'The parameter is incorrect.' is thrown at the line that is calling the GetCurrentVersionAsync() (line 3 in the CheckIfSettingsExists()). I don't understand why this is thrown as even if there was an issue with GetStringAsync() function, I already have it in try block. Can someone shine a light for me why this is happening?

cptalpdeniz
  • 368
  • 1
  • 11
  • have you try to use POST ? because the error is `parameter` i think it requires a parameter `PostStringAsync("/URL", parameter);` – Cyrille Con Morales Dec 22 '22 at 03:57
  • 1
    I think the error is happening in those last two lines, if you move everything in the try block do you still this behaviour? dynamic JsonObj = JsonConvert.DeserializeObject(vatisJSON); return JsonObj["LatestVersion"].ToString(); – Ricky Gummadi Dec 22 '22 at 03:59
  • check the last 2 lines – Ayudh Dec 22 '22 at 04:03
  • 3
    Did you miss the `await` before `Controller.GetCurrentVersionAsync();`? or is this just a typo? – Charles Han Dec 22 '22 at 04:35
  • 1
    Please post the complete exception, including stack trace. – Stephen Cleary Dec 22 '22 at 04:43
  • Indeed that was the issue... @CharlesHan can't believe I missed such a simple thing. Thank you. I'll delete this question now. – cptalpdeniz Dec 22 '22 at 05:17
  • I also wonder why thats the reason, can you explain? – cptalpdeniz Dec 22 '22 at 05:17
  • 1
    This post probably gives better explanation than mine https://stackoverflow.com/questions/5383310/catch-an-exception-thrown-by-an-async-void-method – Charles Han Dec 22 '22 at 06:02
  • 1
    I think we all made this kinda mistake before. @cptalpdeniz Do you mind if I put this into the answer so it will help others to find it in the future? – Charles Han Dec 22 '22 at 06:29
  • of course, if you think that's the best thing to do, please do so. I'm a learner and would consider myself as newbie in this platform so whatever you think is the best, please do that. – cptalpdeniz Dec 22 '22 at 06:47

0 Answers0