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,
- public class Program
- {
- public static void Main(string[] args)
- {
- var host = new WebHostBuilder()
- .UseKestrel()
- .UseContentRoot(Directory.GetCurrentDirectory())
- .UseStartup<Startup>()
- .Build();
- host.Run();
- }
- }
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.
- public class Startup
- {
- public void Configure(IApplicationBuilder app)
- {
-
- }
- public void ConfigureServices(IServiceCollection services)
- {
-
- }
- }
Startup Class Constructor
We can also specify constructor of the startup class. The startup class has constructor with one or three parameters.
- public Startup(IHostingEnvironment env)
- {
-
- }
-
- public Startup(IApplicationBuilder appenv, IHostingEnvironment env, ILoggerFactory loggerFactory)
- {
-
- }
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.
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddMvc();
- }
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.
- public void Configure(IApplicationBuilder app)
- {
- app.UseMvc();
-
- app.Run(context => {
- return context.Response.WriteAsync("Hello Readers!");
- });
- }
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.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
- {
- loggerFactory
- .AddConsole()
- .AddDebug();
-
- app.UseMvc();
-
- app.Run(context => {
- var logger = loggerFactory.CreateLogger("TodoApi.Startup");
- return context.Response.WriteAsync("Hello Readers!");
- });
- }
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.