Tuesday 25 August 2020

Main method in asp.net core

 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.

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

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .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.

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.

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    { }

    public void Configure(IApplicationBuilder appIHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.Run(async (context=>
        {
            await context.Response.WriteAsync("Hello World!");
        });
    }
}

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

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives