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.
The IMiddlewareFactory is a new extensibility point for middleware activation that would be registered with the application's container. The default IMiddlewareFactory should be registered as a scoped service, and (maybe) only resolve middlewareTypes that are already registered in the container.
Hosting will probably need to add the default IMiddlewareFactory to the container, but the implementation can stay in HttpAbstractions.
namespaceMicrosoft.AspNetCore.Http{/// <summary>/// Provides methods to create middlware./// </summary>publicinterfaceIMiddlewareFactory{/// <summary>/// Creates a middleware instance for each request./// </summary>/// <param name="middlewareType">The concrete <see cref="Type"/> of the <see cref="IMiddleware"/>.</param>/// <returns>The <see cref="IMiddleware"/> instance.</returns>IMiddlewareCreate(TypemiddlewareType);/// <summary>/// Releases a <see cref="IMiddleware"/> instance at the end of each request./// </summary>/// <param name="middleware">The <see cref="IMiddleware"/> instance to release.</param>voidRelease(IMiddlewaremiddleware);}/// <summary>/// Defines middleware that can be added to the application's request pipeline./// </summary>publicinterfaceIMiddleware{/// <summary>/// Request handling method./// </summary>/// <param name="context">The <see cref="HttpContext"/> for the current request.</param>/// <param name="next">The delegate representing the remaining middleware in the request pipeline.</param>/// <returns>A <see cref="Task"/> that represents the execution of this middleware.</returns>TaskInvoke(HttpContextcontext,RequestDelegatenext);}}
The typed UseMiddleware extension methods would check if the registered type implements the new IMiddleware interface. If it does, it will automatically decide to use the IMiddlewareFactory to resolve the IMiddleware instead of using the existing middleware activation logic. This will likely be implemented by registering a Func<RequestDelegate, RequestDelegate> that resolves the IMiddlewareFactory from HttpContext.RequestServices.
The existing middleware activation logic will remain for convention-based middleware to preserve backwards compatibility.
The
IMiddlewareFactoryis a new extensibility point for middleware activation that would be registered with the application's container. The defaultIMiddlewareFactoryshould be registered as a scoped service, and (maybe) only resolvemiddlewareTypes that are already registered in the container.Hosting will probably need to add the default
IMiddlewareFactoryto the container, but the implementation can stay in HttpAbstractions.The typed
UseMiddlewareextension methods would check if the registered type implements the newIMiddlewareinterface. If it does, it will automatically decide to use theIMiddlewareFactoryto resolve theIMiddlewareinstead of using the existing middleware activation logic. This will likely be implemented by registering aFunc<RequestDelegate, RequestDelegate>that resolves theIMiddlewareFactoryfromHttpContext.RequestServices.The existing middleware activation logic will remain for convention-based middleware to preserve backwards compatibility.