Pages

Sunday, August 23, 2020

ASP.NET Core Main Method

 namespace whatsupchat
{
    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>();
                });
    }

The Main method is the entry point (where execution will start) when the application starts running. 

The Main method is used to create a host builder, specifically a default builder. 

A builder, knows how to create a web host. 

The default builder also sets up some of the default services behind the scenes, to configure application using the Startup class (webBuilder.UseStartup<Startup>()). 

Build() the builder, gives us a web host instance that knows how to listen for connections and process/receive HTTP messages. Finally, the web host to Run(). 

Once we call Run(), everything that happens in the application is determined by the code that is contained in the Startup class. 

ASP.NET Core now knows that it’s going to instantiate the Startup class and invoke two methods. 

The first method is ConfigureServices, will allow us to add our own custom services in ASP.NET Core. This will then inject those services into pages, controllers, and wherever else we might need them. 

The second is the Configure method, will determine what middleware it will execute for every incoming HTTP message.  

The request pipeline comprises of a series of middleware components. Each middleware component performs some operation on the HttpContext, and then invokes the next middleware in the pipeline or terminates the request. 

Can create custom middleware components. 

No comments:

Post a Comment