Friday, October 18, 2019

How Asp.net Core Application Starts as a Console Application and Converts to a Web Application | Main Method in Asp.net Core | Part-3

 Hello and As-Salaam U Alikum! , In this article we will learn about the main method in Asp.net Core.



In the Previous articles we have created so many asp.net core projects but I didn't emphasis on a file which name is "Program.cs" . Yes we are going to discuss it now. 


whether you open any type of project e.g. (mvc,empty,web api etc) ,you will surely find this file in your solution explorer. just double click it , you will find two static methods in that file .


  1. Main()
  2. CreateWebHostBuilder()
As we can see CreateWebHostBuilder is called in Main method.First we will  discuss role of main method. As we know that a console application always has a main method from where the execution of program code begins. similarly, Asp.net core application also begin as a console application . The role of main method is to configure the application and starts it as web application . I know this is strange to all .net developers but keep patience , we will understand it in later articles that why it works like it.

As we can clearly see that there is a method CreateWebHostBuilder() main method. This method returns an object that implements the IWebHostBuilder  interface.

object of IWebHostBuilder  initially calls build() method which build a web host and host our asp.net core web application on that web host.

secondary it calls the Run() method which starts the application for listening incoming http requests.


what CreateDefaultBuilder does ?


  1. Establishing Web Server 
  2. Application & host Configuration from different configuration sources.
  3. Configuring Logging
OK, now lets discuss hosting point of view. Asp.net Core hosting can be done in two ways .

  1. In Process
  2. Out Process



While establishing a webhost "Startup.cs" class also configured using extension method of IWebHostBuilder   .
















n the startup.cs file we have two methods.

  1. ConfigureServices()
  2. Configure()

The ConfigureServices() method of the startup class configures the services that are required by the application. The Configure() method of the startup class sets the application's pipeline of request processing. In a later article, we will discuss both of these methods in detail.

0 Comments:

Post a Comment

Do not Add Spam links in the Comment Box

Subscribe to Post Comments [Atom]

<< Home