You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 20, 2018. It is now read-only.
I was reading the documentation about the middleware, and I saw something really strange : to create a Middleware, we can write a class with an Invoke method
public interface IMiddleware
{
Task Invoke(HttpContext context);
}
The problem is that we can call the function UseMiddleware with any class, and if the class doesn't contains the correct method we get the error at runtime instead of compilation. And I think the reflection code will be less performant than calling an interface method (didn't benchmark, I will trust you if you say no)
After browsing the source code I understood that we can use dependency injection in the Invoke method (like in the constructor), and it may be the reason we don't have a IMiddleware.
But this is not documented, and I don't think it will be used by many people. I even looked at several default aspnet middlewares, and I didn't see Dependency injection used there (it is always used in the constructor)
Is it really a good idea to loose the benefits of having an Interface in favor of an undocumented barely used functionnality ? Are there other benefits that I didn't see ?
The text was updated successfully, but these errors were encountered:
The DI scenario does discourage the use of interfaces, and the reflection perf hasn't been a concern since the reflection is resolved once at startup rather than per request.
I was reading the documentation about the middleware, and I saw something really strange : to create a Middleware, we can write a class with an Invoke method
But we don't have an interface to define this.
https://docs.asp.net/en/latest/fundamentals/middleware.html#writing-middleware
The interface would be just something like this :
The problem is that we can call the function UseMiddleware with any class, and if the class doesn't contains the correct method we get the error at runtime instead of compilation. And I think the reflection code will be less performant than calling an interface method (didn't benchmark, I will trust you if you say no)
After browsing the source code I understood that we can use dependency injection in the Invoke method (like in the constructor), and it may be the reason we don't have a IMiddleware.
But this is not documented, and I don't think it will be used by many people. I even looked at several default aspnet middlewares, and I didn't see Dependency injection used there (it is always used in the constructor)
Is it really a good idea to loose the benefits of having an Interface in favor of an undocumented barely used functionnality ? Are there other benefits that I didn't see ?
The text was updated successfully, but these errors were encountered: