DotNet Core - suniladhya/Advantage GitHub Wiki

Startup Class ConfigureServices Setup various services, and the components that the application might require the

configure sets up the application request pipeline

Generic Host: for all application type Webhost: For backward compatibility

Host class encapsulates all App resources-services and the middleware, DI, Logging

Iconfiguration is injected by the host. Host can also be injected in the startup class

services added in the configure services method can be injected in the Configure method

The Configure Method is responsible to handle the http request pipeline. 1(https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-5.0) 2(https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/extensibility?view=aspnetcore-5.0) 3(https://docs.microsoft.com/en-us/aspnet/core/performance/performance-best-practices?view=aspnetcore-5.0) 4(https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-3.1#built-in-middleware) Middleware is software that's assembled into an app pipeline to handle requests and responses. Each component:

  • Chooses whether to pass the request to the next component in the pipeline.
  • Can perform work before and after the next component in the pipeline.

The Middleware are chained and follow the "AOP" / Decorator Pattern

app.Run(async context =>await context.Response.WriteAsync("Hello World"));
app.Run(async context =>await context.Response.WriteAsync("Hello Again!")); //doesn't Execute as the http request pipeline is terminated 

Three methods to configure the middlware

1. Terminal delegate: App.Run

  • to indicate the terminal of the http request pipeline.
  • it has only one parameter-Context

2. Chaining Delegate: App.use

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.Use(async (context, next) =>
        {
            // Do work that doesn't write to the Response.
            await next.Invoke();
            // Do logging or other work that doesn't write to the Response.
        });
     }
}

The Above Code can be embedded to a custom Middleware.

Steps:

  1. Create a public Class
  2. implement interface "IMiddlWare"/ Convention based Middleware
  3. include the class in the ConfigureService method
  4. invoke the Middleware (app.UseMiddleWare)

3. Branching Middleware: App.Map

  • Any Moment we want to branch the request to two different Pipeline.
  • when we use the Map, it completely breaks the original pipeline of the request.
  • and Acts as a terminal
  • app.Map, app.MapWhen => when these methods are called, no further call is made to the Run()
  • App.UseWhen() can be used to call the subsequent Run()

Microsoft Image

Filters

Allow code to run before or after request processing pipelines Built in Filters: Authorization, Response caching

Custom filters can be used for cost cutting, logging