-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Background and Motivation
Today RequestDelegateFactory has Create methods that take Delegate. However, there are situations where we also want to wrap a RequestDelegate in a new request delegate. For example, the endpoint has request filters.
RequestDelegateFactory.Create(Delegate, ...) is currently used for both types of delegates. However, it is beneficial to have separate overloads for RequestDelegateFactory.Create(Delegate, ...) and RequestDelegateFactory.Create(RequestDelegate, ...).
The RequestDelegate overload doesn't need to use System.Linq.Expressions to build a request filter pipeline. Apps that only map request delegate endpoints can be statically analyzed not to use expressions and the expressions assembly can be trimmed on publish.
Proposed API
namespace Microsoft.AspNetCore.Http;
public static class RequestDelegateFactory
{
+ public static RequestDelegateResult Create(RequestDelegate handler, RequestDelegateFactoryOptions? options);
}We don't need InferMetadata method or RequestDelegateMetadataResult overloads because request delegates don't have parameters or return values to gather metadata from.
Usage Examples
if (options?.RouteBuilder.Filters.Count > 0)
{
// Endpoint is configured with filters. Wrap the request delegate in a new request delegate that calls the filters.
requestDelegate = RequestDelegateFactory.Create(requestDelegate, options).RequestDelegate;
}Alternative Designs
The alternative is to build the filter pipeline for a RequestDelegate outside of the delegate factory in the Microsoft.AspNetCore.Routing project. However, because it is a separate project from RequestDelegateFactory, there is some duplication between routing and HTTP extensions projects.
See #46020 for an example of messiness.