Asp.Net Core startup configuration

 The Startup class configures services and the app's request pipeline. Startup class contained in Startup.cs in the root folder of the project. ASP.NET Core application must include Startup class. It is like Global.asax in the traditional .NET application. As the name suggests, it is executed first when the application starts.

ASP.NET Core apps use a Startup class, which is named Startup by convention. The Startup class:

- Optionally includes a ConfigureServices method to configure the app's services. A service is a reusable component that provides app functionality. Services are registered in ConfigureServices and consumed across the app via dependency injection (DI) or ApplicationServices.

- Includes a Configure method to create the app's request processing pipeline.



ConfigureServices and Configure are called by the ASP.NET Core runtime when the app starts:

public class Startup

{

    public Startup(IConfiguration configuration)

    {

        Configuration = configuration;

    }


    public IConfiguration Configuration { get; }


    public void ConfigureServices(IServiceCollection services)

    {

        services.AddRazorPages();

    }


    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

    {

        if (env.IsDevelopment())

        {

            app.UseDeveloperExceptionPage();

        }

        else

        {

            app.UseExceptionHandler("/Error");

            app.UseHsts();

        }

        app.UseHttpsRedirection();

        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>

        {

            endpoints.MapRazorPages();

        });

    }

}

ConfigureServices()

The Dependency Injection pattern is used heavily in ASP.NET Core architecture. It includes a built-in IoC container to provide dependent objects using constructors.

A ConfigureServices method is a place where you can register your dependent classes with the built-in IoC container. After registering dependent class, it can be used anywhere in the application. You just need to include it in the parameter of the constructor of a class where you want to use it. The IoC container will inject it automatically.

ASP.NET Core refers dependent class as a Service. So, whenever you read "Service" then understand it as a class which is going to be used in some other class.

The ConfigureServices method includes IServiceCollection parameter to register services to the IoC container. Learn more about it in the next chapter.

public void ConfigureServices(IServiceCollection services)

{

    services.AddMvc(); 

}

Configure()

The Configure method is a place where you can configure application request pipeline for your application using IApplicationBuilder instance that is provided by the built-in IoC container.

ASP.NET Core introduced the middleware components to define a request pipeline, which will be executed on every request. You include only those middleware components which are required by your application and thus increase the performance of your application.

The following is a default Configure method.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)

{

    if (env.IsDevelopment())

    {

        app.UseDeveloperExceptionPage();

    }


    app.Run(async (context) =>

    {

        await context.Response.WriteAsync("Hello World!");

    });

}

Configure method includes three parameters IApplicationBuilder, IHostingEnvironment, and ILoggerFactory by default. These services are framework services injected by built-in IoC container.

At run time, the ConfigureServices method is called before the Configure method. This is so that you can register your custom service with the IoC container which you may use in the Configure method.


Publishing ASP.NET Core Applications for IIS 

https://www.binkod.in/2020/08/deploy-aspnet-core-application-to-iis.html

Share this:

ABOUT THE AUTHOR

Binkod | A technical blog that helps you with problem-solving solutions. Encourage you to be in touch with the latest technologies. We at BinKod do carry a philosophy that: I'm Impossible.

0 comments

If you have any questions or concerns, please let me know.