Program.cs in ASP.NET Core

In this article, we will look at the role of program.cs in ASP.NET Core.  It is the entry point of our app, where we configure the host. We also configure and register required services and configure middleware pipelines & endpoints.

The program class has changed in .NET 6.0. Visit program class in older versions of ASP.NET Core to read about the older version. You can also refer to the startup class.

What is Program.cs ?

The Program class is the entry point for the ASP.NET Core application. It contains the application startup code.

It is where we

  • Configure and register the services required by the app
  • Register middleware components and configure the app’s request-handling pipeline.

What is a host?

On startup, ASP.NET Core apps configure and launch a host.

You can think of a host as a wrapper around your application. It is responsible for the startup and lifetime management of the application. The host contains the application configuration and the Kestrel server (an HTTP server) that listens for requests and sends responses. It also sets up the logging, dependency injection, configuration, request processing pipeline, etc.

There are three different hosts capable of running an ASP.NET Core app.

  • WebApplication (or Minimal Host)
  • Generic Host
  • WebHost

WebHost was used in the initial versions of ASP.NET Core. Generic Host replaced it in ASP.NET Core 3.0. WebApplication was introduced in ASP.NET Core 6.0.

What is WebApplication (Minimal Host)

The WebApplication is the core of your ASP.NET Core application. It contains the application configuration and the HTTP server (Kestrel) that listens for requests and sends responses.

WebApplication behaves similarly to the Generic Host, exposing many of the same interfaces but requiring fewer configure callbacks.

We need to configure the WebApplication before we run it. There are two essential tasks that we need to perform before running the app.

  1. Configure and add the services for dependency injection.
  2. Configure the request pipeline, which handles all requests made to the application.

We do all these things in the program.cs

Understanding program.cs

Create a new ASP.NET core application using the ASP.NET Core Empty template. Open the program.cs. You will see the following lines.

These four lines contain all the initialization code you need to create a web server and start listening for requests.

The first thing you notice in this file is that no main method exists. The main method is the entry point of every C# program. But C#9 introduced the top-level statements feature, where you do not have to specify a main method. This feature allows us to create one top-level file containing only statements. It will not have to declare the main method or namespace. That top-level file will become the entry point of our program.

The program file creates the web application in three stages. Create, Build, and Run

Program file Create Build and Run

The earlier versions of ASP.NET Core created two files. One is program.cs, and the other is startup.cs (known as startup class). The program.cs is where we configured the host, and the startup class is where we used to configure the application. Since ASP.NET core 6, both processes are simplified and merged into a program.cs.

Create

The first line of the code creates an instance of the WebApplication builder.

The CreateBuilder is a static method of WebApplication class. It sets up a few basic features of the ASP.NET platform by default, including

  1. Sets up HTTP server (Kestrel)
  2. Logging
  3. Configuration
  4. Dependency injection container
  5. Adds the following framework-provided services

This method returns a new instance of the WebApplicationBuilder class.

We use the WebApplicationBuilder object to configure & register additional services.

WebApplicationBuilder exposes the ConfigurationManager type, which exposes all the current configuration values. We can also add new configuration sources.

It also exposes a IServiceCollection. We use this to add services to the DI container.

We then call the build method.

Build

The build method of the WebApplicationBuilder class creates a new instance of the WebApplication class.

We use the instance of the WebApplication to set up the middleware’s and endpoints.

The template has set up one middleware component using the MapGet extension method.

Run

The run method of the WebApplication instance starts the application and listens to http requests.

Program.cs in Various Project Templates

.NET SDK Provides several project templates for creating a new ASP.NET core application. The most commonly used templates are

  • ASP.NET Core Empty
  • ASP.NET Core Web App (Razor pages)
  • ASP.NET Core Web App (Model-View-Controller)
  • ASP.NET Core Web API

ASP.NET Core Web App

The following program class is from the template ASP.NET Core Web App (Razor pages).

The code uses the extension method AddRazorPages to register Razor page-related services. Note that it is added before calling the build method.

After the build method, several middleware components are added.

ASP.NET Core Web App (Model View Controller)

The following program class is from the ASP.NET Core Web App (Model View Controller) template.

ASP.NET Core Web API

The following program class is from the ASP.NET Core Web Api template.

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