Wednesday 23 September 2020

ASP.NET Core Startup Class

Global.asax is no more in ASP.NET Core application. Startup.cs file is a replacement of Global.asax file in ASP.NET Core. Startup class triggers at first when application launches.

The Startup class is a place where:

  1. Services are configured which are required by the application .
  2. The app's request handling pipeline is defined, as a series of middleware components.

Description.
Startup.cs is mandatory in the application, it can be decorated with any access modifier like public, private, internal. multiple Startup classes are allowed in a single application. ASP.NET Core will select the appropriate class based on its Enviroment.

If a class Startup{EnvironmentName} exists, that class will be called for that EnvironmentName.
Should we need to define class name with startup.cs? No it is not necessary that class name should be Startup.
We can define two method in startup file like ConfigureServices and Configure.

public class Startup
{
    // Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
    …
    }
    
    // Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app)
    {
    …
    }
}

ConfigureServices Method :
This is an optional method in Startup class which is used to configure services for application. when any request come to application ConfigureService method will be called first.
ConfigureServices method includes IServiceCollection parameter to register services. 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 :
The Configure method is used to specify how the application will respond in each HTTP request. this method is mostly used for registering middleware in HTTP pipeline. this method method accept IApplicationBuilder parameter along with some other services like IHostingEnvironment and ILoggerFactory. Once we add some service in ConfigureService method, it will be available to Configure method to be used.

public void Configure(IApplicationBuilder app)
{
app.UseMvc();
}

Above example shows to enable MVC feature in our framework we need to register UseMvc() into Configure and also need to register service AddMvc() into ConfigureServices.

0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives