ASP.NET CORE Main Method

In this tutorial will discuss and understand what is asp.net core main method and below points.

        • Importance and use of ASP.NET Core Main method.
        • What happen in background when .NET Core application is executed.

In an ASP.NET Core project we have a file with name Program.cs. In this file we have asp.net core main method as public static void Main() method

public class Program
{
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    
}

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.

Importance and use Main method.

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 look at the body of the Main() method, then you will find that its call to the CreateHostBuilder() method by passing the command line arguments args as an argument.

public static void Main(string[] args)
{
    CreateHostBuilder(args).Build().Run();
}

So, when the runtime executes our application it looks for this Main() method and this where the execution starts.

What happen in background when .NET Core application is executed

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.

As you can see, CreateWebHostBuilder() method returns an object that implements IHostBuilder. On this object, Build() method is called which builds a web host that hosts our asp.net core web application.

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

CreateDefaultBuilder() method creates a web host with pre-configured defaults. CreateDefaultBuilder() method does several things to create a web host. We will discuss all that the CreateDefaultBuilder() method does in detail in our next video. For now, just understand that the CreateDefaultBuilder() method sets up a web host with certain defaults.

Startup Class

While setting up the host, the Startup class is also configured using the UseStartup() extension method of the IHostBuilderclass. The Startup class has two methods.

webBuilder.UseStartup();

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.

You can watch our video for the tutorial with step by step explanation