Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 1 RC2 web application entry point

So they changed the way they're bootstrapping web applications between asp.net 5 rc1 and rc2.

It used to be:

public static void Main(string[] args) => WebApplication.Run<Startup>(args);

But in RC2, I no longer have a reference to the static class WebApplication. Any ideas?

like image 424
J-DawG Avatar asked Feb 05 '16 16:02

J-DawG


1 Answers

After reflecting on your question for a while, I think it has three parts.

  1. bootstrapping the application,
  2. the application entry point, and
  3. the change in creating a web host (i.e. this missing WebApplication)

Bootstrapping the application

In rc1 dnx.exe bootstraps your application, whereas in rc2 your compiled console application (foo.exe) does the bootstrapping. By bootstrapping, I mean starting up the managed application by creating a process, loading the Core CLR into RAM, and finding your application's entry point. This required dnx.exe in rc1 because your web application only pretended to be a console app, whereas in rc2 your web application is a real console app, along with its own executable. What makes the console app a web application is that it uses ASP.NET Core libraries.

Application entry point

In both rc1 and rc2 the application entry point is the Main() method. That is where we put the first line of code that we want to run. This makes sense, when we consider that ASP.NET Core runs within a console application, and a console application's default entry point is its Main() method.

The hosting class

In both rc1 and rc2 the Main() method calls into ASP.NET Core libraries, and the first call to ASP.NET Core code creates the ASP.NET Core host. In rc1 we started this with WebApplication.Run<SomeClass>(). In rc2 we instead jump right into adding middleware to the pipeline with a call to new WebHostBuilder().FluentMiddlewareCall()...Build(). That's the ASP.NET Core host that we're calling not the application's managed entry point. This is an important distinction.

Summary

In ASP.NET Core rc2, your application is its own executable with it own bootstrapping code. It's managed entry point is its Main() method. It becomes a web application because it references ASP.NET Core libraries and creates a web host using new WebHostBuilder().

// application entry point
public static void Main(string[] args) 
{ 
    // code here will run before the host. 
    Console.WriteLine("Hello world.");

    // this is the rc1 host call. 
    // WebApplication.Run<Startup>(args); 

    // this is the rc2 host call
    var host = new WebHostBuilder()...

    // you shouldn't do work after the host call.
} 

Historical comparison

In ASP.NET 4.x The Internet Information Services (IIS) executable (InetMgr.exe) loads the Common Language Runtime (CLR) and uses it to create/call a managed web application's entry point. This entry point is the HttpApplication.Application_Start() event, which we can handle via Global.asax to add our first line of code.

InetMgr.exe > Runtime > HttpApplication/Global.asax > Application_Start() 

In ASP.NET Core rc1: The DNX (dnx.exe) executable loads the Core CLR and uses it to create/call our application's Main() method. We call WebApplication and its Run<SomeClass>() method, thereby creating a host and building up an HTTP pipeline.

Dnx.exe > Runtime > Main() > WebApplication.Run<SomeClass>() > ...

In ASP.NET Core rc2 Our application's compiled executable (foo.exe) loads the Core CLR to create/call our applications Main() method. We call WebHostBuilder(), thereby creating a host and building up an HTTP pipeline.

ConsoleApp.exe > Runtime > Main() > WebHostBuilder() > ...

References

https://vimeo.com/153212604

https://blogs.msdn.microsoft.com/dotnet/2015/11/18/announcing-net-core-and-asp-net-5-rc

http://docs.asp.net/en/latest/tutorials/your-first-aspnet-application.html

https://github.com/aspnet/Home/wiki/DNX-structure

like image 70
Shaun Luttin Avatar answered Nov 16 '22 01:11

Shaun Luttin