Program.cs in ASP.NET Core (.NET Core <= 5.0)

This article applies to ASP.NET core version 3.0 to 5.0. You can visit program.cs in ASP.NET core to learn the latest version.

In this article, we will look at the role of asp.net core program.cs in more detail. It is the entry point of our app, where we configure the Web host. The program class configures the application infrastructure like Web host, logging, Dependency injection container, IIS integration, etc. It is created, configured, built using the createdefaultbuilder method of the Main method in the program class.

What is Program.cs?

The Program class contains the method Main, which is the entry point of the ASP.NET Core applications.

The Main method is similar to the Main method of a console Applications. That is because all the .NET Core applications basically are console applications. We build other types of applications like MVC Web Application or Razor page application over the console app.

The main purpose of the Program class is to configure the applications infrastructure.

The program class creates the Web Host at the startup. it then configures the logging, Dependency Injection Container. configuration system, Kestrel Web server, IIS Integration, etc. It also adds the framework services to the DI container so that we can use it. The code for the program class is automatically generated for us and most probably enough for most of the projects.

What is Web Host?

The Web host is responsible for starting the app and running it. It is created when the application starts. Web host creates a server, which listens for the HTTP requests. It configures the request pipeline (or middleware pipeline). Also, It sets up DI container, where we add our services. Managing the lifetime of the services is also a task of Webhost.

Basically, Web Host ready’s our app to receive the requests. But, the Web Host needs to be created and configured. We do that in Main method.

Main method

Open the Applications we built in the Hello World Example and open the program.cs

This class has a single method Main

This is the entry point to our application.

The following image shows how the web host is created.

  1. CreateWebHostBuilder creates the host and provides the configuration to it.
  2. The build method builds it using the provided configuration
  3. The Run method runs it and it then listens for HTTP requests.
Main method in program.cs class
The Main method of Program.cs class in ASP.NET Core

Create the Host

CreateWebHostBuilder

The CreateWebHostBuilder is a static method, which creates and configures the Host and then returns it.

The above syntax is known as “expression bodied function member.” feature that was introduced in C# 

The above code is the same as the following function

The method above uses the WebHost helper static class

The CreateDefaultBuilder method of the WebHost helper class is responsible for creating the IWebHostBuilder instance, with the desired configuration

CreateDefaultBuilder

The CreateDefaultBuilder performs the following tasks.

  1. Sets the content root to Directory.GetCurrentDirectory.
  2. Loads optional configuration from
    1. Appsettings.json
    2. Appsettings.{Environment}.json.
    3. User secrets when the app runs in the Development environment.
    4. Environment variables
    5. Command-line arguments.
  3. Enable logging
  4. Sets up the Dependency Injection Container.
  5. Configures Kestrel as the webserver
  6. Adds Framework Services to the DI Container
  7. Integrates the Kestrel run with IIS

You can peek into the source code for the CreateDefaultBuilder from the ASP.NET Core meta package 

There is a lot of code there. Let us look at each line

Setting the content Root

This sets the content root to Directory.GetCurrentDirectory. This basically sets the current directory as the root of the application.

Loading Configuration file

The next section of the code loads the configuration from the various sources and in the following order

  1. Appsettings.json.
  2. appsettings.{Environment}.json.
  3. User secrets when the app runs in the Development environment.
  4. Environment variables.
  5. Command-line arguments.

Enable Logging

The code which enables & configures logging follows next.

This reads the configuration rules specified in the “Logging” section of the configuration files and configures logging for console and debug output.

Sets up the DI Container

The UseDefaultServiceProvider method sets up the Dependency Injection container and configures it.

Configures WebHost

Finally, the code calls the ConfigureWebDefaults method. This method configures the Web Host. The source code is available on GitHub.

Kestral

The First method tells the host to use the Kestrel Web server. Kestrel is a cross-platform Managed HTTP server-based. This Server allows ASP.NET Core application runs on OS other than Windows.

Configure services

This section configures the important services & adds them to DI Container.

Use IIS Or Integration

Configures IIS Server to Host the App. There are two ways in which you can host the App. One is in process and the other one is out of process.

The in Process runs the app inside the IIS Process and is configured by UseIIS().

The Out of process runs in a separate process and uses the Kestrel server. The IIS then acts as a reverse proxy forwarding the requests to the Kestrel. This is configured by the method UseIISIntegration().

Startup class

The Host is created and configured, but before building & running we need to further configure with the application. We do this in the startup class. By default, the class is named as the startup class. We let the builder know the location of the startup class by using the UseStartup method.

The startup class contains two methods. One is ConfigureServices (which is optional) and the other one is Configure. The ConfigureServices is where we configure services we created in the app and adds them to the DI Container. In the Configure method, we create the request pipeline by adding the middlewares.

The CreateWebHostBuilder will invoke the ConfigureServices & configure method from the startup class and configure the host further

Build & Run it

CreateWebHostBuilder creates the Web Host and returns it back. The Build & Run methods are invoked and the app starts and begins listening for HTTP requests.

Summary

The Main method of the program.cs class is the entry point of our application. It configures & builds the Web host. The web host is responsible for running our app. Most of the plumbing required to configure host is already done for us in the createdefaultbuilder method, which is invoked in the Main method. We can further add our custom configuration is startup class, which we cover in the next tutorial.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top