When a new ASP.NET Core 2.0 project is created, the boilerplate Main
method in the Program
class looks something like this:
public static void Main(string[] args)
{
BuildWebHost(args).Run(); // BuildWebHost returns an IWebHost
}
But since C# 7.1, the Main
method can be an asynchronous method returning Task
instead of void
. This means it's much easier to call an async method inside Main
.
So the RunAsync()
on IWebHost
can be called inside Main
instead of the Run()
method. Something like this:
public static async Task Main(string[] args)
{
await BuildWebHost(args).RunAsync().ConfigureAwait(false);
}
According to the documentation, the Run
method:
Runs a web application and block the calling thread until host shutdown.
Whereas the RunAsync
method:
Runs a web application and returns a Task that only completes when the token is triggered or shutdown is triggered.
I was wondering when should the RunAsync
method be used instead of the regular Run
method? What are the practical implications of this? Would the end-user notice any difference?