# Middleware in Asp.net Core
1. In ASP.NET Core, Middleware is a piece of software that can handle an HTTP request or response. A given middleware component in ASP.NET Core has a very specific purpose.
2. For example we may have a middleware component that authenticates a user, another piece of middleware to handle errors, yet another middleware to serve static files such as JavaScript files, CSS files, Images etc.
3. The request pipeline is configured as part of the application startup by the Configure() method in Startup.cs file.
4. By default empty project template sets up a very simple request processing pipeline with just two pieces of middleware.
- UseDeveloperExceptionPage is one middleware and the second middleware is setup using the Run() method.
5. In ASP.NET Core, a Middleware component has access to both - the incoming request and the outgoing response. So a Middleware component may process an incoming request and pass that request to the next piece of middleware in the pipeline for further processing.
6. A middleware component may handle the request and decide not to call the next middleware in the pipeline. This is called short-circuiting the request pipeline. Short-circuiting is often desirable because it avoids unnecessary work.
- For example, if the request is for a static file like an image or css file, the StaticFiles middleware can handle and serve that request and short-circuit the rest of the pipeline. This means in our case, the StaticFiles middleware will not call the MVC middleware if the request is for a static file.
7. A middleware component may also process the outgoing response. For example, the logging middleware component may log the time the response is sent. In addition it may also calculate the over all time taken to process the request by computing the difference between request received and response sent times.
8. Middleware components are executed in the order they are added to the pipeline. Care should be taken to add the middleware in the right order, otherwise the application may not function as expected.
9. The middleware components are available as NuGet packages. This means updates are now handled by NuGet, providing the ability to update each middleware separately.
## Run middleware
1. a middleware that is registered using the Run() method cannot call the next middleware in the pipeline
2. So, the middleware that we register using Run() method is a terminal middleware
3. If you want your middleware to be able to call the next middleware in the pipeline, then register the middleware using Use() method as shown below.
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Hello from 1st Middleware");
await next();
});
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello from 2nd Middleware");
});
4. Notice, Use() method has 2 parameters. The first parameter is the HttpContext context object and the second parameter is the Func type i.e it is a generic delegate that represents the next middleware in the pipeline.
## if u want to understand how same middleware handles request and response. See below code.
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILogger<Startup> logger)
{
app.Use(async (context, next) =>
{
logger.LogInformation("MW1: Incoming Request");
await next();
logger.LogInformation("MW1: Outgoing Response");
});
app.Use(async (context, next) =>
{
logger.LogInformation("MW2: Incoming Request");
await next();
logger.LogInformation("MW2: Outgoing Response");
});
app.Run(async (context) =>
{
await context.Response.WriteAsync("MW3: Request handled and response produced");
logger.LogInformation("MW3: Request handled and response produced");
});
}
Tuesday, 22 March 2022
Middleware in Asp.net Core
Subscribe to:
Post Comments (Atom)
Search This Blog
Creating your first "Alexa" Skill
Index What is Alexa What is Alexa Skill? Why is it required when Alexa already equipped with voice assistant? Dev...
About Me
Menu
-
Index What is Alexa What is Alexa Skill? Why is it required when Alexa already equipped with voice assistant? Dev...
-
Adding Azure AD B2C to React Native App Register your app in Azure active directory 1. Go to azure ad b2c, app registratio...
-
# Project file 1. .net Core project file no longer contains file or folder reference - all files and folder present within the root fol...
No comments:
Post a Comment