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?
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.