In an ASP.NET Core project we have seen a file with named Program.cs. In this file we have a public static void Main() method.
If you have any experience with previous versions of .NET, a console application has a Main() method and it is the entry point for that console application.
But here, we are creating an asp.net core web application and not a console application. So the obvious question that comes to our mind is why do we have a Main() method.
Well, the important point to keep in mind is that, an asp.net core application initially starts as a console application and the Main() method in Program.cs file is the entry point.
So, when the runtime executes our application it looks for this Main() method and execution starts.
This Main() method configures asp.net core and starts it and at that point it becomes an asp.net core web application.
So, if you take a look at the Main() method, it calls CreateWebHostBuilder() method passing it the command line arguments. https://github.com/WebHost.cs
As you can see, CreateWebHostBuilder() method returns an object that implements IWebHostBuilder.
On this object, Build() method is called which builds a web host that hosts our asp.net core web application.
On the web host Run() method is called, which runs the web application and it begins listening for incoming HTTP requests.
CreateWebHostBuilder() method calls CreateDefaultBuilder() static method of the WebHost class.
CreateDefaultBuilder() method creates a web host with pre-configured defaults. CreateDefaultBuilder() method does several things to create a web host.
As part of setting up a web host, Startup class is also configured using the UseStartup() extension method of IWebHostBuilder class.
By convention, the startup class in ASP.NET Core is named Startup. This class has 2 methods.
Startup class does the following 2 very important things
- ConfigureServices() method configures services required by the application
- Configure() method sets up the application's request processing pipeline
0 comments:
Post a Comment