Skip to content

RouteConstraints aren't linker friendly #24723

@davidfowl

Description

@davidfowl

Today RouteConstraints are described by a RouteOptions.ConstraintMap. The issue with this approach is the linker doesn't have way to know that these types are activated elsewhere in the system. We need an API that lets us describe adding a constraint that is type safe and lets us describe that we want to preserve constructors:

public class RouteOptions
{
    public void AddParameterPolicy<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]T>(string text) where T : IParameterPolicy;
    public void AddParameterPolicy(string text, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]Type constraintType);
}

This would let the linker know that these constraint types should be kept around.

Here's the current warning:

C:\dev\git\aspnetcore\src\Http\Routing\src\ParameterPolicyActivator.cs(93,13): Trim analysis warning IL2006: Microsoft.AspNetCore.Routing.ParameterPolicyActivator.CreateParameterPolicy(IServiceProvider,Type,String): The parameter 'parameterPolicyType' of method 'Microsoft.AspNetCore.Routing.ParameterPolicyActivator.CreateParameterPolicy(IServiceProvider,Type,String)' with dynamically accessed member kinds 'None' is passed into the implicit 'this' parameter of method 'System.Type.GetConstructors()' which requires dynamically accessed member kinds 'PublicConstructors'. To fix this add DynamicallyAccessedMembersAttribute to it and specify at least these member kinds 'PublicConstructors'. [C:\Users\davifowl\source\repos\WebApplication60\WebApplication60\WebApplication60.csproj]

Metadata

Metadata

Assignees

No one assigned

    Labels

    affected-fewThis issue impacts only small number of customersapi-approvedAPI was approved in API review, it can be implementedarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesbugThis issue describes a behavior which is not expected - a bug.feature-routinglinker-friendlinessTracking linker friendlinessseverity-majorThis label is used by an internal tool

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions