Creation of Custom Middleware using IMiddleware Interface in ASP.NET Core Last Updated : 19 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The IMiddleware Interface in ASP.NET Core is a contract for creating a custom middleware that can be added to the application's existing pipeline. This interface was introduced in ASP.NET Core to provide an alternative way to create middleware components, apart from the traditional RequestDelegate-based middleware. The main difference is that IMiddleware supports dependency injection via the constructor more naturally, making it easier to manage and test i.e. unlike traditional middleware, which is generally registered using UseMiddleware<T>() extension, IMiddleware implementation can be registered and resolved directly through the DI container. Moreover, middleware implemented using IMiddleware are transient by nature. A new instance of the middleware is created per request, which can be useful for particular scenarios where you need a fresh state and specific scoped services.Now let's see how to use it:Middleware Class Implementing IMiddleware: Create a class implementing IMiddleware and define the InvokeAsync function to specify the custom middleware's behaviour.Register the Middleware in the DI Container: Register your middleware class in Startup.cs class as a transient service in the ConfigureService method.Use the Middleware in the Pipeline: In Startup.cs class, inside Configure method write app.UseMiddleware<CustomMiddleware>(). C# public class Custom_Middleware: IMiddleware { private readonly IMyService _myService; public Custom_Middleware(IMyService myService) { _myService = myService; } public async Task InvokeAsync(HttpContext context, RequestDelegate next) { //your custom logic here await next(context); } } //In Startup.cs inside ConfigureService method services.AddTransient<Custom_Middleware>(); //In Startup.cs inside Configure method app.UseMiddleware<Custom_Middleware>(); ConclusionThe IMiddleware interface in ASP.NET Core provides a more explicitly DI-friendly way to create custom middleware components. It simplifies dependency injection via constructor injection and allows for better management and testing of middleware. Its transient nature also ensures that a new instance of middleware is created per request, which is useful for handling fresh state and scoped services. Comment More infoAdvertise with us Next Article Creation of Custom Middleware using IMiddleware Interface in ASP.NET Core C choudhary3o2l Follow Improve Article Tags : C# CSharp ASP-NET Netcore Similar Reads Why to Use ASP.NET Server Controls in Place of HTML Controls? Basically HTML controls are client side controls and ASP.NET controls are server side controls. We prefer ASP.NET controls as our web controls. As with the HTML controls we can't maintain the state ie the data is lost, we can say it as it does not provide state management. And while writing the code 4 min read Middleware in ASP.NET Core In this article, we will understand what middleware is in asp.net core. A Middleware is a very broad term in asp.net core middleware is a piece of software that can handle an HTTP request or response. For example, we may have a middleware of a component that authenticates a user, another piece of mi 4 min read Basic CRUD (Create, Read, Update, Delete) in ASP.NET MVC Using C# and Entity Framework Prerequisites: Download and Install Microsoft SQL Server Management StudioDownload and Setting Up Visual Studio Community Version MVC stands for Model View Controller. It is a design pattern that is employed to separate the business logic, presentation logic, and data. Basically, it provides a patte 6 min read ASP.NET Interview Questions and Answer ASP.NET is a popular framework by Microsoft for building fast and scalable web applications. It allows developers to create dynamic websites, services, and apps, using server-side code and offering a user-friendly experience. Trusted by companies like Microsoft, Dell, and Accenture, ASP.NET is used 15+ min read Explain ConfigureServices and Configure method in ASP.NET ? In this article, we will see what is Configure and ConfigureService methods. Basically, these two methods play a very important role in startup class. The baseline of this class is to reduce the dependency of the application on the server. Actually, The Startup class is required to define two method 8 min read Like