I was recently in a technical discussion with a senior developer and was asked:
What is the difference between RequestDelegate and Middleware in ASP.NET Core?
I have already visited the following links:
My answer:
RequestDelegate is the building block of the request pipeline. It is a delegate (visit GitHub for full source code) that represents a method which is capable of processing an instance of HttpContext (HTTP request). The middleware contains a private readonly RequestDelegate _next field which points to the next middleware's Invoke method, which gets invoked when await _next(context) is executed in the current middleware. And this process continues until the end of the pipeline or the request is short-circuited.
Middleware:
My answer to this was almost similar to the RequestDelegate answer with the following differences:
- We can define middleware in-line as an anonymous method — to which he mentioned that this anonymous method is request delegate, then where is the middleware?
- We can define middleware in a reusable class — to which he mentioned that this reusable class has an
Invokemethod matching the delegate signature and everything it does is within thisInvokemethod. So, where is the middleware and how would you define it?
Now I am confused and my question to you is: What is the difference between RequestDelegate and Middleware?
Is it just a class that has an implementation of Invoke method?
In your answer, if you can provide examples of code to explain the two and their boundaries, when does one starts and other ends or vice versa, that would be great too and it will help me visualize the difference?
UPDATE 20th Jul, 2025
The MSDN documentation provides several instances where the term RequestDelegate and Middleware is used interchangeably:
An individual request delegate can be specified in-line as an anonymous method (called in-line middleware), or it can be defined in a reusable class. These reusable classes and in-line anonymous methods are middleware, also called middleware components...
If we visit this link, we will see a diagram/image showing multiple blue blocks marked as Middleware 1, Middleware 2 and so on, but the wording used is RequestDelegate:
The ASP.NET Core request pipeline consists of a sequence of request delegates, called one after the other. The following diagram demonstrates the concept. The thread of execution follows the black arrows...
And if we visit this link, we will see another diagram/image showing the order of execution of middleware components and the wording used here is Middleware:
The following diagram shows the complete request processing pipeline for ASP.NET Core MVC and Razor Pages apps. You can see how, in a typical app, existing middlewares are ordered and where custom middlewares are added