ASP.NET Core 3.1 Main Method
In this article, I am going to discuss the ASP.NET Core 3.1 Main Method in detail. Please read As part of this article, we are going to discuss the following three important pointers in detail.
- The Significance of the ASP.NET Core Main Method.
- Why do we have a Main() Method in ASP.NET Core?
- What happens behind the scenes when you run a .NET core application?
In order to understand the Main method of ASP.NET Core Web application, let’s first create an Empty ASP.NET Core Web application.
Creating Empty ASP.NET Core Web Application using ASP.NET Core 3.1
To create a new Empty ASP.NET Core Web Application, open Visual Studio 2019, and then click on the Create a new project box as shown in the below image.
Once you click on the Create a new project box, it will open the following Create a new project window. From this window, select the ASP.NET Core Web Application Project Template and then click on the Next button as shown in the below image.
Once you click on the Next button, it will open the following Configure Your New Project window. Here, you need to give an appropriate name for your project, set the location where you want to create this project, and the solution name for the ASP.NET Core Web application. Here, I am giving the project name “FirstCoreWebApplication” and then clicking on the Create button as shown in the image below.
Once you click on the Create button, it will open the Create a new ASP.NET Core Web Application window. From this window, first, select .NET Core and ASP.NET Core 3.1 and then select the Empty Project template and uncheck all the checkboxes from the Advanced section and finally click on the Create button as shown in the below image.
Once you click on the Create button, it will take some time and will create the Empty ASP.NET Core Web Application with the following file and folder structure.
As you can see from the above image, we have a class file with the name Program.cs. The Program.cs class file of our ASP.NET Core Web Application contains the following code.
From the above image, you can see that the Program class contains a public static void Main() method. As we already know, when we create a console application in .NET, then by default the .NET Framework creates a class (i.e. Program class) with the Main Method. We also know that the Main method is the entry point for that console application execution.
Now the question that should come to your mind is, here we are not creating a console application, here we create an ASP.NET Core Web Application. Then why do we have a Main() method in the ASP.NET Core Web Application?
Why do we have a Main() Method in ASP.NET Core Web Application?
The most important point that you need to keep in mind is, the ASP.NET Core Web Application initially starts as a Console Application and the Main() method is the entry point to the application. So, when we execute the ASP.NET Core Web application, first it looks for the Main() method and this is the method from where the execution starts. The Main() method then configures ASP.NET Core and starts it. At this point, the application becomes an ASP.NET Core Web application.
If you further look at the body of the Main() method, then you will find that it makes a call to the CreateHostBuilder() method by passing the command line arguments args as an argument as shown in the below image.
As shown in the below image, the CreateHostBuilder() method returns an object that implements the IHostBuilder interface. The Host is a static class that can be used for creating an instance of IHostBuilder with pre-configured defaults. The CreateDefaultBuilder() static method of the Host class creates a new instance of the HostBuilder with pre-configured defaults. Internally, it configures Kestrel (Internal Web Server for ASP.NET Core), IISIntegration, and other configurations.
Within the Main() method, on this IHostBuilder object, the Build() method is called which actually builds a Web Host. Then it hosts our ASP.NET Core Web application within that Web Host. Finally, on the Web Host, it is called the Run() method, which will actually run the Web Application and it starts listening to the incoming HTTP requests.
The CreateHostBuilder() method calls the static CreateDefaultBuilder() method on the Host class. The CreateDefaultBuilder() method creates a Web Host with the default configurations. Behind the scene, to create a Host, the CreateDefaultBuilder() method does several things. In the next article, we will discuss the CreateDefaultBuilder() Method in Detail. For now, just understand that the CreateDefaultBuilder() method sets up a web host with default configurations.
Startup Class in ASP.NET Core 3.1
While setting up the host, the Startup class is also configured using the UseStartup() Extension method of the IHostBuilder class. The Startup class has two methods as shown in the below image.
The ConfigureServices() method of the Startup class configures the services which are required by the application. The Configure() method of the Startup class sets up the pipeline of the application’s request processing. In a later article, we will discuss these two methods in detail.
Note: From .NET 6, we don’t have the above Startup class. Configuring the services and configuring the Request Processing Pipeline are moved to the Main method of the Program class. In our upcoming articles, we will discuss these things in detail.
Here, in this article, I try to explain the ASP.NET Core 3.1 Main Method. I hope you understood the need and use of the Main Method in ASP.NET Core 3.1 Web Application.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.