Understanding Startup Class In ASP.NET Core

Introduction

This class is described by its name: startup. It is the entry point of the application. It configures the request pipeline which handles all requests made to the application. The inception of startup class is in OWIN (Open Web Interface for.NET) application that is specification to reduce dependency of application on server.

Is Startup Class mandatory?

Yes, this class is mandatory in ASP.net core application. It can have any access modifier (public, private, internal). This is the entry point of the ASP.net application. It contains application configuration related items.

Must the name of class be "Startup"?

No, it is not necessary that the class name be "Startup". The ASP.net core application is a Console app and we have to configure a web host to start listening. The "Program" class does this configuration.

Following is sample code for Program class,

  1. public class Program  
  2. {  
  3.     public static void Main(string[] args)  
  4.     {  
  5.             var host = new WebHostBuilder()  
  6.             .UseKestrel()  
  7.             .UseContentRoot(Directory.GetCurrentDirectory())  
  8.             .UseStartup<Startup>()  
  9.             .Build();  
  10.             host.Run();  
  11.     }  
  12. }  
The web host is created in the main method of the Program class and here we have configuration of startup class using method "UseStartup". So it is not necessary that class name is "Startup".

Following is a typical startup class example.

  1. public class Startup   
  2. {  
  3.     public void Configure(IApplicationBuilder app)  
  4.     {  
  5.       
  6.     }  
  7.     public void ConfigureServices(IServiceCollection services)  
  8.     {  
  9.   
  10.      }       
  11. }  
Startup Class Constructor 

We can also specify constructor of the startup class. The startup class has constructor with one or three parameters.

  1. public Startup(IHostingEnvironment env)      
  2. {   
  3.   
  4. }  
  5.   
  6. public Startup(IApplicationBuilder appenv, IHostingEnvironment env,  ILoggerFactory loggerFactory)  
  7. {  
  8.   
  9. }  
IApplicationBuilder is an interface that contains properties and methods related to current environment. It is used to get the environment variables in application.

IHostingEnvironment is an interface that contains information related to the web hosting environment on which application is running. Using this interface method, we can change behavior of application.

IloggerFactory is an interface that provides configuration for the logging system in Asp.net Core. It also creates the instance of logging system.

The startup class contains two methods: ConfigureServices and Configure.

ConfigureServices Method

Declaration of this method is not mandatory in startup class. This method is used to configure services that are used by the application. When the application is requested for the first time, it calls ConfigureServices method. This method must be declared with a public access modifier, so that environment will be able to read the content from metadata.

ASP.net core has built-in support for Dependency Injection. We can add services to DI container using this method. Following are ways to define ConfigureServices method in startup class.

  1. public void ConfigureServices(IServiceCollection services)  
  2. {  
  3.         services.AddMvc();  
  4. }   
In case of specific class or library of project that would like to add to DI container, we will use the IserviceCollection. In the above example, I have just add MVC service to the ConfigureServices method.

Configure Method

This method is used to define how the application will respond on each HTTP request i.e. we can control the ASP.net pipeline. This method is also used to configure middleware in HTTP pipeline. This method accept IApplicationBuilder as a parameter. This method may accept some optional parameter such as IHostingEnvironment and ILoggerFactory. Whenever any service is added to ConfigureServices method, it is available to use in this method.

  1. public void Configure(IApplicationBuilder app)  
  2. {  
  3.     app.UseMvc();  
  4.   
  5.     app.Run(context => {  
  6.         return context.Response.WriteAsync("Hello Readers!");  
  7.     });  
  8. }  
In the above example, I have added "UseMvc" method, so system will know MVC framework need to use to handle user requests. With this method, we can also pass some additional option parameters.
  1. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)  
  2. {  
  3.     loggerFactory  
  4.         .AddConsole()  
  5.         .AddDebug();  
  6.   
  7.         app.UseMvc();  
  8.     
  9.     app.Run(context => {  
  10.         var logger = loggerFactory.CreateLogger("TodoApi.Startup");  
  11.         return context.Response.WriteAsync("Hello Readers!");  
  12.     });  
  13. }  
In the above example, I have added the logger console and the debug logger and MVC service. The app.Run method adds a terminal middleware delegate to the application's request pipeline.

Summary

The Startup class is mandatory and it is the entry point of the application. With the help of this class we can configure the environment in our ASP.net Core application. We can use Constructor and two different methods: ConfigureServices and Configure for setting up the environment. This class creates services and injects services as dependencies so the rest of the application can use these dependencies. The ConfigureServices used to register the service and Configure method allow us to add middleware and services to the HTTP pipeline. This is the reason ConfigureServices method calls before Configure method.

Up Next
    Ebook Download
    View all
    Learn
    View all