Search code examples
c#jsonweb-servicesasynchronoustomtom

Awaiting method causes application to exit


I've tried to have a look online for an answer to this and I'm not finding anything.

This method I have looks fine, and seems to follow the general flow of how async post calls work in c#.

     private static async Task<HttpResponseMessage> GetGeocodingAsync(string URL, string JSONQuery)
    {
        using (HttpClient client = new HttpClient())
        {
            using (HttpResponseMessage r = await client.PostAsync(URL, new StringContent(JSONQuery, Encoding.UTF8, "application/json")))
            {
                return r;
            }
        }
    }

    public Task<HttpResponseMessage> GetGeocoding(string TTURL, string JSONQuery)
    {
        return GetGeocodingAsync(TTURL, JSONQuery);
    }

When I check my syntax and in my IDE however when I run the application it gets as far as line 5 then the application just ends with a code 0 (exited with code 0 (0x0).).

I've done a lot of web research and can't find an answer for this. Am I missing something fundamental here?

I understand that I have to use an private static async for the actual post part, then I can call this method from a non-static non-async method, and in my main class I can process the response that I get like this:

   Task<HttpResponseMessage> x = TTConnect.GetGeocoding(TTConnect.GetTTConnectionURL(),JSONQuery);

I'm trying to send some data in JSON format using POST, check the response by parsing it, and pull down the response to the POST.

I'm using the TomTom Batch API

Any thoughts or suggestions would be welcome.


Solution

  • I believe you're problem is the way you're using async / await and the Task in general.

    This line

    Task<HttpResponseMessage> x = TTConnect.GetGeocoding(TTConnect.GetTTConnectionURL(),JSONQuery);
    

    ... needs to either be in an async method and call await or .Result on the task. x is not the result of the task but the task itself. Task is just a managed thread right? What you're doing is assigning x the thread so to speak and not the result of what the thread has done (...only it's not a thread it's a task but anyway).

    In order to get x to be the result of the task you would do something like...

    var x = await TTConnect.GetGeocoding(TTConnect.GetTTConnectionURL(),JSONQuery);
    

    or

    var x = TTConnect.GetGeocoding(TTConnect.GetTTConnectionURL(),JSONQuery).Result;
    

    Another thing to point out is that your private static GetGeocodingAsync method is the same as your public GetGeoCoding method. You can put the .Result in the public method return line and it will work. OR you can just delete it and use the GetGeocodingAsync method with the .Result or await it.

    Anyway, the point is you're not using tasks correctly and I think there's just a little bit of confusion on how / what a task is and how to properly use it normally or with async await. Not a big deal either. Just fix the code to call it properly and it will work properly (either throwing errors or not for example.)