Tuesday, February 12, 2019

How does ASP.NET Core Application initially starts?

ASP.NET Core application initially starts as a console application
  • It consists of Main method in its Program class which is the entry point 
  • Program class is located inside the root directory of the project

Main Method

  • Main method configures ASP.NET Core

public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); }

  • IWebHostBuilder, the return type of the WebHost.CreateDefaultBuilder has methods that define a web server and the startup class
  • The web server extension methods such as UseKestrel() for Kestrel server, UseIIS() for IIS server, UseHttpSys() for HTTPSys server, can be used
  • Kestrel web server is default server used by framework
    • Kestrel is a cross-platform web server for ASP.NET Core
    • It is the default web server included by ASP.NET Core project templates
  • The UseStartup method specifies the Startup class for your application
public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); }
  • The Startup class is where any services required by the app are configured and the request handling pipeline is defined.
  • The Build and Run methods build the IWebHost object that hosts the app and begins listening for HTTP requests

No comments:

Post a Comment