Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`await HttpClient.GetAsync()` failing silently - DNX command line app [duplicate]

The following fails silently on the indicated line.

using (var ht = new HttpClient())
{
    var r = await ht.GetAsync(RootUrl + Id.ToString()); <-----
    Page = await r.Content.ReadAsStringAsync();
}

No exception, no error, no nothing. Execution just stops, doesn't hit a breakpoint on the next line, and the app exits. Surrounding in a try/catch doesn't trigger a catch either.

Running as a DNX .xproj commandline app.

Same behavior with both coreclr and full. Started both through VS, and DNX command line.

What the heck is going on?

Project.json is as follows:

{
    "version": "1.0.0-*",
    "description": "Scrapers Console Application",
    "authors": [ "Kaelan" ],
    "tags": [ "" ],
    "projectUrl": "",
    "licenseUrl": "",

    "dependencies": {

        "System.Net.Http": "4.0.0-*",
        "HtmlAgilityPack": "1.4.9"
    },

  "commands": {
    "Scrapers": "Scrapers"
  },

  "frameworks": {
    "dnx451": { }
    }
}

Edit: Full code i'm working with. Took the chunk of the actual project to test, (maintains the same issue).

public class Program
{
    const string RootUrl = "<-snipped->";
    public async void Main(string[] args)
    {
        var Id = 244;

        var Page = "";

        using (var ht = new HttpClient())
        {
            var r = await ht.GetAsync(RootUrl + Id.ToString());
            Debug.Write(r.StatusCode + " on page: " + Id.ToString());

            Page = await r.Content.ReadAsStringAsync();
        }

        Console.ReadLine();
    }
}
like image 476
Kaelan Fouwels Avatar asked Sep 02 '25 16:09

Kaelan Fouwels


1 Answers

You should avoid async void.

Change the return type to the appropriate asynchronous return type for methods without result values: Task.

like image 101
Stephen Cleary Avatar answered Sep 04 '25 04:09

Stephen Cleary