New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AddRoutingCore(this IServiceCollection services) and RouteOptions updates for CreateSlimBuilder #46428
Comments
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
Related to #46227 |
@eerhardt @mitchdenny @davidfowl @DamianEdwards Looking at #46227 (comment), it seems like there's disagreement on whether any constraints belong in AddRoutingCore() and/or CreateSlimBuilder(). I think AddRoutingCore() being the exact same as AddRouting() but only without the regex constraint is very arbitrary. Maybe if we called it AddRoutingWithoutRegexContstraint() it'd be more understandable 😆. Based on discussions in the web frameworks sync today, it seems like we're mostly on board to have AddRoutingCore() not include any constraints by default which would make adding a new AddDefaultRouteConstraints() API desirable. The slim builder could still add all the defaults except the RegexInlineRouteConstraint using internal API if we feel that's important for usability. Would everyone be okay with that? |
Backing up a bit, the whole reason we are doing this is the reduce the binary size when using To be honest, I'm going a bit cold on removing the other constraints. From a usability perspective I think having to tell people to go hunting for the constraint type names which is something that they don't really need to deal with otherwise seems worse than just having a doc somewhere that says add this one line for regex. I'm updating the PR now with the proposed changes -- removing all constraints and adding |
This PR removes all the constraints: |
Update this issue for completeness as it looks like we going with |
We have Core suffixes in the stack. Slim is not a common suffix and I don’t think we should diverge (I’m also hoping Slim won’t stick on the builder).
|
The reason I like AddRoutingSlim() instead of AddRoutingCore() is because of its relation to CreateSlimBuilder(). Considering CreateSlimBuilder() calls this API, and the main purpose behind both APIs is to be AOT and trim friendly while being as functional as possible out of the box, I think reusing "Slim" here makes a lot of sense. "Core" is pretty meaningless, and we overuse that term. AddMvcCore() and AddSignalRCore() both claim to add "the minimum essential MVC/SignalR services", and there's nothing minimum or essential about AddRoutingCore(). It's exactly the same as AddRouting without regex constraint support by default. If we were to remove all constraints, the AddRoutingCore() naming might make more sense. |
I've converted the PR that implements this back to |
Agreed. We've spun our wheels enough on this one. API Review Notes:
API approved as proposed! namespace Microsoft.Extensions.DependencyInjection;
public static class RoutingServiceCollectionExtensions
{
+ public static IServiceCollection AddRoutingCore(this IServiceCollection services);
} |
merged. |
Background and Motivation
Modify
RouteOptions
to not automatically add theRegexInlineRouteConstraint
. InsteadAddRouting(...)
is modified to call a new method calledAddRoutingCore(...)
which does everything thatAddRouting(...)
does, but then adds theRegexInlineRouteConstraint
back in toRouteOptions
.Proposed API
namespace Microsoft.Extensions.DependencyInjection; public static class RoutingServiceCollectionExtensions { + public static IServiceCollection AddRoutingCore(this IServiceCollection services); }
Usage Examples
This usage of
CreateSlimBuilder()
will result in no regex constraint being available:... the above code would run, but when a request is made to
/products/abcd1234
it would throw anInvalidOperationException
. We could consider adding an analyzer which detects when CreateSlimBuilder(...) is being used and detect a route pattern with a regex constraint and tell people that they need to add it (possibly with a fixer).If you do want to add the regex route constraint back in, this is what it would look like:
Alternative Designs
Rather than using
SetParameterPolicy<T>(string)
we considered adding a specific method calledAddRegexConstraint(...)
but it is somewhat over specific. We've also considered changes to Regex to reduce the impact it has on binary size by eliminating backtracking (saves around 800kb).We have not fully explored using regex source generator support (with the exception of the alpha constraint). The regex source generator relies on a Regex being defined as a field and then the source generator substituting the initialization of that field with a method that returns the source generated Regex instance. This won't work with the regex constraint because the string is passed around as a parameter at runtime.
Once the request delegate source generator is further along we might find a way to achieve this and we can revisit.
Risks
Could be somewhat confusing as to why we are excluding just the reg-ex constraint.
The text was updated successfully, but these errors were encountered: