3

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();
    }
}
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
Kaelan Fouwels
  • 1,155
  • 1
  • 13
  • 28
  • Post executable repro code. – usr Aug 29 '15 at 22:22
  • Doesn't add a whole lot, but see edited – Kaelan Fouwels Aug 29 '15 at 22:33
  • Shouldn't there be a compiler warning for async Main? Btw, the repo did point to the solution. See the answer. – usr Aug 29 '15 at 22:48
  • You would think so.. – Kaelan Fouwels Aug 29 '15 at 22:58
  • @usr: This isn't a normal console Main. It's a DNX-hosted console, with an instance (not static) `Main` method, which actually allows `async Task Main` methods. So the compilers don't treat it special at all (yet). I have a [blog post exploring this a bit](http://blog.stephencleary.com/2015/03/async-console-apps-on-net-coreclr.html). – Stephen Cleary Aug 29 '15 at 23:02
  • @StephenCleary did you relocate the blog? I have subscribed previously but I was not notified of the recent articles you posted. My Feedly also doesn't seem to know the blog. – usr Aug 29 '15 at 23:04
  • @usr: I went to [feedburner](http://feeds.feedburner.com/NitoPrograms) quite a while ago, but the [original location](http://blog.stephencleary.com/feed.xml) should have stayed the same. The blog transition wasn't quite seamless, sorry! – Stephen Cleary Aug 29 '15 at 23:10

1 Answers1

3

You should avoid async void.

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

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810