HttpClient.PostAsync knocks out the app with exit code 0

Your problem is that a program normally exits when its Main() method finishes. And your Main() finishes as soon as you hit the await in Run(), because that’s how async methods work.

What you should do is to make Run() into an async Task method and then wait for the Task in your Main() method:

static void Main()
{
    RunAsync().Wait();
}

private static async Task RunAsync()
{
    …
}

In C# 7.1+ you should use async Main instead:

static async Task Main()
{
    await RunAsync();
}

private static async Task RunAsync()
{
    …
}

Few more notes:

  1. You should never use async void methods, unless you have to (which is the case of async event handlers).
  2. Mixing await and Wait() in a GUI application or in ASP.NET is dangerous, because it leads to deadlocks. But it’s the right solution if you want to use async in a console application.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)