My FeedDiscussionsHashnode Enterprise
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Hemant Singh

1 like

·

156 reads

3 comments

Jubril Adebiyi
Jubril Adebiyi
Sep 19, 2023

very insightful

·
Ajmal Muhammed
Ajmal Muhammed
Sep 19, 2023

Will invokeAsync take other arguments other than httpcontext? Like what if I want a custom middleware to be called next ? Can I pass that middleware into invokeAsync function?

·
·1 reply
Hemant Singh
Hemant Singh
Author
·Sep 21, 2023

InvokeAsync can take any number parameters, as long as they are properly mapped using Dependency Injection. It is similar to a constructor in behavior that way. However, we should not clutter this method by injecting too many objects, unless they really add to the purpose of the middleware class.

We should not pass a middleware as one of the parameters in InvokeAsync, as you can easily define the sequence of calling middlewares in Startup or Program class. Right where we map our error handling middleware using app.UseMiddleware<ErrorHandlingMiddleware>(), we can write in the next line app.UseMiddleware<MyMiddleware>(), and that will ensure that MyMiddleware is next in line.

·