IWebHost: Call Run () vs RunAsync ()

When a new ASP.NET Core 2.0 project is created, the template method Mainin the class Programlooks something like this:

public static void Main(string[] args)
{
    BuildWebHost(args).Run(); // BuildWebHost returns an IWebHost
}

But since C # 7.1, a method Maincan be an asynchronous method returning Taskinstead void. This means that it is much easier to call the asynchronization method internally Main.

So, RunAsync()on IWebHostcan be called internally Maininstead of a method Run(). Something like that:

public static async Task Main(string[] args)
{
    await BuildWebHost(args).RunAsync().ConfigureAwait(false);
}

According to the documentation method Run:

Launches a web application and blocks the calling thread until the host shuts down.

While the method RunAsync:

Launches a web application and returns a task that ends only when a token is triggered or triggered.

, RunAsync Run? ? - ?

+6
2

ASP.NET Main:

public static void Main(string[] args)
{
    BuildWebHost(args).Run();
}

Run WebHostExtensions.Run , :

public static void Run(this IWebHost host)
{
    host.RunAsync().GetAwaiter().GetResult();
}

, WebHostExtensions.RunAsync .


, Main # 7.1s :

[ ] , , :

  • static Task Main() , private static void $GeneratedMain() => Main().GetAwaiter().GetResult();
  • static Task Main(string[]) , private static void $GeneratedMain(string[] args) => Main(args).GetAwaiter().GetResult();

, , Main:

public static async Task Main(string[] args)
{
    await BuildWebHost(args).RunAsync();
}

:

private static void $GeneratedMain(string[] args)
{
    Main(args).GetAwaiter().GetResult();
}

, , , WebHostExtensions.Run.

? , . , . . , , , Main; , , , - , , ASP.NET Core (.. Startup, ).

+3

? ?

RunTime.

CLR, - . . . Async Main Roslyn Git.
- # 7 , 2: Async Main

0

All Articles