0

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 Invoke method matching the delegate signature and everything it does is within this Invoke method. 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

1 Answer 1

0

The difference between Middleware and RequestDelegate is the difference between a subject and an object of the verb "to decorate".

A Middleware decorates the RequestDelegate.

Sign up to request clarification or add additional context in comments.

2 Comments

Your answer could be right, but I did not understand it. Could you elaborate a bit more? "A Middleware decorates the RequestDelegate", how?
Are you familiar with the decorator pattern (as per Gang of Four)? A class that exposes an interface and also takes that interface as a property. The decorator applies a behaviour before wrapping the inner interface. In this example the “interface” is RequestDelegate. The middleware wraps a RequestDelegate with another RequestDelegate. So more properly the middleware is a decorator factory. Take the Authentication middleware. It will wrap a RequestDelegate with another RequestDelegate, which will do auth logic then mutate the HttpContext.User before passing control to the

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.