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();
- }
- }
Following is a typical startup class example.
- public class Startup
- {
- public void Configure(IApplicationBuilder app)
- {
- }
- public void ConfigureServices(IServiceCollection services)
- {
- }
- }
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)
- {
- }
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();
- }
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!");
- });
- }
- 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!");
- });
- }
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.
No comments:
Post a Comment