In this article, I am going to discuss the ASP.NET Core Main Method in detail. Please read our previous article where we discussed the ASP.NET Core Project File in detail. As part of this article, we are going to discuss the following two important concepts in detail.
Creating ASP.NET Core Web Application
Open Visual Studio 2017. Then select File => New => Project from the file menu as shown below.
From the “New Project” window, from the left pane expand the “Installed” template section. Then expand the “Visual C#” section and then select .NET Core. From the middle pane, you need to choose ASP.NET Core Web Application. Provide a meaningful name for your application such as “FirstCoreWebApplication”. Then provide the location where you want to create the Project. Finally, click on the OK button as shown in the image below.
Once you click on the OK button, it will open the following window. From the below window, select ASP.NET Core 2.2 (currently it is the latest version). Then uncheck the Configure for HTTPS checkbox. Then click on the OK button as shown below.
Once you click on the OK button, it will create the ASP.NET Core Web Application with the following structure.
As you can see from the above image, we have a file with the name Program.cs. The Program.cs 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 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?
The most important point that you need to keep in mind is, the ASP.NET Core 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 application, first it looks for the Main() method and this is the method from where the execution starts. The Main() method then configures the ASP.NET Core and starts it. At this point, the application becomes an ASP.NET Core web application.
If you look at the body of the Main() method, then you will find that it makes a call to the CreateWebHostBuilder() method by passing the command line arguments as shown in the below image.
As shown in the below image, the CreateWebHostBuilder() method returns an object that implements the IWebHostBuilder interface.
Within the Main() method, on this IWebHostBuilder 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, we called the Run() method, which actually runs the web application and it starts listening to the incoming HTTP requests.
CreateWebHostBuilder() method calls the static CreateDefaultBuilder() method of the WebHost class. The CreateDefaultBuilder() method creates a web host with the default configurations.
Behind the scene, to create a web 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
While setting up the web host, the Startup class is also configured using the UseStartup() extension method of the IWebHostBuilder class. The Startup class has two methods as shown below.
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.
In the next article, I will discuss the ASP.NET Core InProcess Hosting in detail. Here, in this article, I try to explain the ASP.NET Core Main Method in detail. I hope you understood the need and use of the Main Method in ASP.NET Core Web Application.
Summary:
I hope this post will be helpful to understand the concept of ASP.NET Core Main Method
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
- 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?
Creating ASP.NET Core Web Application
Open Visual Studio 2017. Then select File => New => Project from the file menu as shown below.
From the “New Project” window, from the left pane expand the “Installed” template section. Then expand the “Visual C#” section and then select .NET Core. From the middle pane, you need to choose ASP.NET Core Web Application. Provide a meaningful name for your application such as “FirstCoreWebApplication”. Then provide the location where you want to create the Project. Finally, click on the OK button as shown in the image below.
Once you click on the OK button, it will open the following window. From the below window, select ASP.NET Core 2.2 (currently it is the latest version). Then uncheck the Configure for HTTPS checkbox. Then click on the OK button as shown below.
Once you click on the OK button, it will create the ASP.NET Core Web Application with the following structure.
As you can see from the above image, we have a file with the name Program.cs. The Program.cs 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 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?
The most important point that you need to keep in mind is, the ASP.NET Core 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 application, first it looks for the Main() method and this is the method from where the execution starts. The Main() method then configures the ASP.NET Core and starts it. At this point, the application becomes an ASP.NET Core web application.
If you look at the body of the Main() method, then you will find that it makes a call to the CreateWebHostBuilder() method by passing the command line arguments as shown in the below image.
As shown in the below image, the CreateWebHostBuilder() method returns an object that implements the IWebHostBuilder interface.
Within the Main() method, on this IWebHostBuilder 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, we called the Run() method, which actually runs the web application and it starts listening to the incoming HTTP requests.
CreateWebHostBuilder() method calls the static CreateDefaultBuilder() method of the WebHost class. The CreateDefaultBuilder() method creates a web host with the default configurations.
Behind the scene, to create a web 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
While setting up the web host, the Startup class is also configured using the UseStartup() extension method of the IWebHostBuilder class. The Startup class has two methods as shown below.
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.
In the next article, I will discuss the ASP.NET Core InProcess Hosting in detail. Here, in this article, I try to explain the ASP.NET Core Main Method in detail. I hope you understood the need and use of the Main Method in ASP.NET Core Web Application.
Summary:
I hope this post will be helpful to understand the concept of ASP.NET Core Main Method
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉