Creation of Custom Middleware using IMiddleware Interface in ASP.NET Core Last Updated : 19 Sep, 2024 Comments Improve Suggest changes 3 Likes 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. Create Quiz Comment C choudhary3o2l Follow 3 Improve C choudhary3o2l Follow 3 Improve Article Tags : C# CSharp ASP-NET Netcore Explore IntroductionC# Tutorial 2 min read Introduction to .NET Framework 6 min read C# .NET Framework (Basic Architecture and Component Stack) 6 min read C# Hello World 2 min read Common Language Runtime (CLR) in C# 4 min read FundamentalsC# Identifiers 2 min read Data Types in C# 6 min read C# Variables 4 min read C# Literals 5 min read Operators in C# 7 min read C# Keywords 5 min read Control StatementsC# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch) 5 min read C# Switch Statement 4 min read Loops in C# 4 min read C# Jump Statements (Break, Continue, Goto, Return and Throw) 4 min read OOP ConceptsClass and Objects in C# 4 min read Constructors in C# 5 min read C# Inheritance 3 min read Encapsulation in C# 2 min read C# Abstraction 4 min read MethodsMethods in C# 4 min read Method Overloading in C# 4 min read Method Parameters in C# 4 min read Method Overriding in C# 7 min read Anonymous Method in C# 2 min read ArraysArrays in C# 6 min read Jagged Arrays in C# 4 min read Array Class in C# 5 min read How to Sort an Array in C# | Array.Sort() Method Set - 1 8 min read How to find the rank of an array in C# 2 min read ArrayListArrayList in C# 6 min read ArrayList Class in C# 4 min read C# | Array vs ArrayList 2 min read StringStrings in C# 6 min read C# Verbatim String Literal - @ 5 min read C# String Class 9 min read C# StringBuilder 2 min read C# String vs StringBuilder 3 min read TupleC# Tuple 7 min read C# Tuple Class 3 min read C# ValueTuple 7 min read C# ValueTuple Struct 4 min read IndexersC# Indexers 5 min read C# Multidimensional Indexers 5 min read C# - Overloading of Indexers 3 min read Like