Sunday, February 10, 2019

Asp.Net Core Startup Configuration

Startup configuration for Asp.Net Core applications are defined in a startup file Startup.cs. This is similar to the global.aspx.cs file in the previous version of Asp.Net. Startup settings are important in Asp.Net Core since we need to define the execution pipeline during startup to handle all incoming requests.

There are 2 important methods in the Startup which define the execution sequence of Asp.Net core request execution.

Configure
ConfigureServices


Configure
The Configure methods is where we define the execution pipeline of the application. The request pipeline is configured by adding middleware components to an IApplicationBuilder instance. IApplicationBuilder is available to the Configure method, but it isn't registered in the service container.

Middleware components are configured using a sequence of Use methods like UseBrowserLink, UseStaticFiles, UseMvc etc. Each middleware component in the request pipeline is responsible for invoking the next component in the pipeline. Additional services, such as IHostingEnvironment and ILoggerFactory, may also be specified in the method signature. When specified, additional services are injected if they're available.

The basic syntax of the Configure method is as follows.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.Use...();
}

ConfigureServices
The ConfigureServices is an optional method in the Startup, which is used to inject services which can be used in the Application. The ConfigureServices method is called by the runtime before the Configure() method.

The ConfigureServices method gets an IServiceCollection object as parameter, all the services to be injected to the application are added to this collection by calling a set of Add() methods like AddDbContext, AddIdentity, AddMvc etc

The basic syntax of the ConfigureService method is as follows.

public void ConfigureServices(IServiceCollection services)
{
    services.Add...();
}


Search Flipkart Products:
Flipkart.com

No comments: