Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between next(context) vs await next.Invoke(context) in .net core middleware creation [duplicate]

As per my understanding, we need to use the next(context) to call the next request delegate/middleware. I have gone through multiple online resources, but have not found an exact difference between next(context) and next.Invoke(context).

I want to use next(context) in place of next.Invoke(context) in existing code written by another person based on my learning. If I am using next() instead of next.Invoke(context), everything is working fine. I want to know if it will impact anything or there is any difference between these two calls.

like image 659
DSR Avatar asked Sep 07 '25 00:09

DSR


1 Answers

next is a RequestDelegate in this context, hence there is no functional difference between next(context) and next.Invoke(context). next(context) will be translated to next.Invoke(context).

Please note that you'll need to await next(context) in the same situations as with next.Invoke(context).

More info on custom middleware: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-5.0

like image 123
Roar S. Avatar answered Sep 08 '25 14:09

Roar S.