관리자 (IP : 18.104.22.168)
[ASP.NET Core] [스크랩] How to use factory-based middleware activation in ASP.NET Core
When building an ASP.NET Core application, you can draw on various middleware components to inspect, route, or modify the request and response messages that flow through the pipeline. You can also write your own custom middleware in ASP.NET Core.
Usually, you have a chain of middleware components in the application pipeline in ASP.NET Core. In this article, we’ll examine middleware in ASP.NET Core and how we can work with the convention-based and factory-based approaches.
To use the code examples provided in this article, you should have Visual Studio 2022 installed in your system. If you don’t already have a copy, you can download Visual Studio 2022 here.
Create an ASP.NET Core 7 Web API project in Visual Studio 2022
First off, let’s create an ASP.NET Core 7 project in Visual Studio 2022. Follow these steps:
We’ll use this ASP.NET Core 7 Web API project to work with factory-based middleware activation in the sections below.
Understanding middleware in ASP.NET Core
Middleware are the software components that comprise the request/response processing pipeline in ASP.NET Core. Incoming requests flow through each middleware component in the pipeline, and each of those components can either process the request or forward it to the next component in the pipeline.
There are lots of things middleware can do, including authentication, authorization, logging, exception handling, routing, caching, and response compression. You can modularize the functionality of your application into individual components and add, remove, or rearrange middleware to tailor the request and response processing pipeline to your application.
Traditionally, you had to configure your middleware components using the UseMiddleware extension method in the Startup class or the Program.cs file when using a newer version of ASP.NET Core. In contrast, factory-based middleware activation lets you define and configure middleware components using factories, which provide greater flexibility in activation.
Note that if you don’t write your custom middleware factory class, the default middleware factory will be used. In ASP.NET Core, you can activate your middleware in two different ways: convention-based middleware activation and factory-based middleware activation. Let’s now examine both approaches.
Convention-based middleware in ASP.NET Core
Convention-based middleware activation in ASP.NET Core is a feature that allows you to automatically apply middleware to the request/response pipeline based on predefined conventions, rather than explicitly configuring each middleware component. The following code listing illustrates how you can create a convention-based middleware component.
You can add this middleware to the request processing pipeline usng the following piece of code in the Program.cs file.
Factory-based middleware in ASP.NET Core
Factory-based middleware activation in ASP.NET Core provides a more flexible and dynamic way to configure and activate middleware components. With factory-based middleware activation, you can customize the middleware instantiation process based on your application’s needs.
Factory-based middleware allows you to inject dependencies having a scoped lifetime using the constructor of the your middleware class—a feature not supported by convention-based middleware. A scoped lifetime means that the service is created once per client request, and disposed at the end of the request.
Factory-based middleware has the following advantages over traditional-style or convention-based middleware:
To use factory-based middleware activation, you should follow the four steps outlined below.
The following code listing illustrates how you can write a factory-based middleware component.
You should register the middleware in the services container using the following piece of code.
You can now add this middleware much the same way we added middleware in the convention-based middleware example.
Each call to the UseMiddleware extension method checks whether or not the middleware implementation in question conforms to the IMiddleware interface. If this condition is met, the instance of IMiddlewareFactory registered in the service container is used to resolve the IMiddleware interface implementation instead of the convention-based middleware implementation. This middleware is registered as a transient or scoped service within the services container.
The IMiddlewareFactory interface defines two methods, namely the Create(Type) and Release(IMiddleware) methods. While the Create(Type) method is used to create a middleware instance for each request, the Release(IMiddleware) method releases an IMiddleware instance at the end of a request. The default implementation of the IMiddlewareFactory is available in the Microsoft.AspNetCore.Http.MiddlewareFactory class.