Understand the ASP.NET Core fundamentally (1)- Program.cs


Well, I gotta say, ASP.NET Core is definitely interesting. Thought it is s a bit late to discuss it now as this thing has been around for quite sometime. But if you are like me that late to this technology and intend to understand the fundamental, this article is right for you.

It is always a good idea to understand the basic structure of the file system and get the overall idea before using it in the first place. So I think it is necessary to understand those default files that generated while you create a new ASP.NET Core project. According to the MSDN documentation. These are the files and folders created by default:

  • Startup.cs : Startup Class – class configures the request pipeline that handles all requests made to the application.
  • Program.cs : Program Class that contains the Main entry point of the application.
  • [project_name].csproj : Project file MSBuild Project file format for ASP.NET Core applications. Contains Project to Project references, NuGet References and other project related items.
  • appsettings.json / appsettings.Development.json : Environment base app settings configuration file. See Configuration.
  • bower.json : Bower package dependencies for the project.
  • .bowerrc : Bower configuration file which defines where to install the components when Bower downloads the assets.
  • bundleconfig.json : configuration files for bundling and minifying front-end JavaScript and CSS assets.
  • Views : Contains the Razor views. Views are the components that display the app’s user interface (UI). Generally, this UI displays the model data.
  • Controllers : Contains MVC Controllers, initially HomeController.cs. Controllers are classes that handle browser requests.
  • wwwroot : Web application root folder.

The two most important files to discuss here are Program.cs & Startup.cs. 


The Program.cs has a Main() method that kicks off the entire ASP.NET Core process, when you launch a ASP.NET Core application, it starts from here. Think of .NET Core as a console application like an executable. The entire service and function are only activated when you launch the application which here is the starting point. Hence, within the Main() method in Program.cs, there are many steps for configuring and initializing web service related setup.

 public class Program
    public static void Main(string[] args)
        var host = new WebHostBuilder()


The Main() method instantiates the WebHostBuilder class to configure the .NET Core web server. You can tell from here that the WebHostBuilder is the main component that dictates the entire application configuration. It is using a builder pattern that step by step to create the IWebHost object by calling the build() method lastly. What builder pattern does is just creating a custom object with its own self-defined step by step process, you can find out more from builder pattern

So let’s take a look of these steps for building the web host IWebHost object.

First of all, you need to understand that the WebHostBuilder is an implementation of  IWebHostBuilder, it has many features that could expand into another post, but we only focus on the methods are shown in the code snippet.



After instantiate the WebHostBuilder , it first calls UseKestrel() to specify a default web server, this is the default ASP.NET Core cross-platform web server that listens all the HTTP requests and surfaces them to your application as set of request features composed into an HttpContext.



The second step here, it calls the UseStartup() method by passing a Startup type class that defined in the Startup.cs file, which is another interesting file we need to explore later. The purpose of this method and the Startup class here can refer to the MSDN as follows

The Startup class is where you define the request handling pipeline and where any services needed by the app are configured.

So basically, define the request handling pipeline is for middleware and configure service needed is for dependency injection.


Finally, it calls the Build() method to return the IWebHost instant and this instant calls the method to kick off the web host server completely.



To summarize the ASP.NET Core launch process, we start from the Program.cs Main() method as an entry point, within the Main() method, we instantiate a WebHostBuilder object and configure the instant step by step for the web host server includes passing the Startup class that defines the request handling pipeline (middleware etc) and configures any services (dependency injection) needed by the app. Finally, the WebHostBuilder object run the Build() method to create another IWebHost instant which calls a Run() method itself to kick off the application host server.