Skip to content

Two Map* methods on the same source line cause InvalidCastException with Request Delegate Generator #48383

@martincostello

Description

@martincostello

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

If the Request Delegate Generator is used in an application where, for some reason, there are more than one Map*() methods on the same line of source code, the application will fail to start with an InvalidCastException exception.

This is because the metadata is only keyed on the source file name and line, and not also the span within that line.

This is very much an edge-case.

While strange and me intentionally breaking it from looking how the code works, I thought it worth logging just in case there's some metaprogramming out there that might generate code that's compact and not-very-human-readable that stumbled into this somehow.

Expected Behavior

The application works.

Steps To Reproduce

Compile and run the following application with the Request Delegate Generator enabled:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/this", () => Results.Json("is")); app.MapGet("/an-edge-case", (string value) => Results.Json(value));

app.Run();

Exceptions (if any)

InvalidCastException: Unable to cast object of type 'System.Func`1[Microsoft.AspNetCore.Http.IResult]' to type 'System.Func`2[System.String,Microsoft.AspNetCore.Http.IResult]'.
Microsoft.AspNetCore.Http.Generated.<GeneratedRouteBuilderExtensions_g>F69328E0708B4B584C5AACA22FE2C51A1CF192D6622828F613FC57C583CA77B63__GeneratedRouteBuilderExtensionsCore+<>c.<.cctor>b__8_3(Delegate del, RequestDelegateFactoryOptions options, RequestDelegateMetadataResult inferredMetadataResult) in GeneratedRouteBuilderExtensions.g.cs
Microsoft.AspNetCore.Routing.RouteEndpointDataSource.CreateRouteEndpointBuilder(RouteEntry entry, RoutePattern groupPrefix, IReadOnlyList<Action<EndpointBuilder>> groupConventions, IReadOnlyList<Action<EndpointBuilder>> groupFinallyConventions)
Microsoft.AspNetCore.Routing.RouteEndpointDataSource.get_Endpoints()
Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.CreateEndpointsUnsynchronized()
Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.EnsureEndpointsInitialized()
Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.get_Endpoints()
Microsoft.AspNetCore.Routing.DataSourceDependentCache<T>.Initialize()
System.Threading.LazyInitializer.EnsureInitializedCore<T>(ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory)
Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher..ctor(EndpointDataSource dataSource, Lifetime lifetime, Func<MatcherBuilder> matcherBuilderFactory)
Microsoft.AspNetCore.Routing.Matching.DfaMatcherFactory.CreateMatcher(EndpointDataSource dataSource)
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.InitializeCoreAsync()
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.<Invoke>g__AwaitMatcher|9_0(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task<Matcher> matcherTask)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

.NET Version

8.0.100-preview.4.23260.5

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    NativeAOTarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-rdg

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions