Skip to content

InvalidOperationException when using AsParameters with FromForm and IEnumerable properties #62329

Open
@marcominerva

Description

@marcominerva

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

With the following endpoint:

app.MapPost("/api/documents", ([AsParameters] DocumentUploadRequest request) =>
{
    return TypedResults.Ok();
});

public class DocumentUploadRequest
{
    public IFormFile File { get; set; }

    [FromForm]
    public IEnumerable<int> Values { get; set; }
}

I get an exception when running the project:

An unhandled exception has occurred while executing the request.
      System.InvalidOperationException: variable 'Values_local' of type 'System.Collections.Generic.IEnumerable`1[System.Int32]' referenced from scope '', but it is not defined

The same errors happens if I use IList, List, and so on. If instead I use int[], it works correctly.

Exceptions (if any)

An unhandled exception has occurred while executing the request.
      System.InvalidOperationException: variable 'Values_local' of type 'System.Collections.Generic.IEnumerable`1[System.Int32]' referenced from scope '', but it is not defined
         at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)
         at System.Linq.Expressions.Compiler.VariableBinder.VisitParameter(ParameterExpression node)
         at System.Linq.Expressions.ExpressionVisitor.VisitMemberBinding(MemberBinding node)
         at System.Linq.Expressions.ExpressionVisitor.Visit[T](ReadOnlyCollection`1 nodes, Func`2 elementVisitor)
         at System.Linq.Expressions.ExpressionVisitor.VisitMemberInit(MemberInitExpression node)
         at System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node)
         at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)
         at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)
         at System.Linq.Expressions.Compiler.VariableBinder.Bind(LambdaExpression lambda)
         at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
         at System.Linq.Expressions.Expression`1.Compile()
         at Microsoft.AspNetCore.Http.RequestDelegateFactory.HandleRequestBodyAndCompileRequestDelegateForForm(Expression responseWritingMethodCall, RequestDelegateFactoryContext factoryContext)
         at Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(Delegate handler, RequestDelegateFactoryOptions options, RequestDelegateMetadataResult metadataResult)
         at Microsoft.AspNetCore.Routing.RouteEndpointDataSource.CreateRouteEndpointBuilder(RouteEntry entry, RoutePattern groupPrefix, IReadOnlyList`1 groupConventions, IReadOnlyList`1 groupFinallyConventions)
         at Microsoft.AspNetCore.Routing.RouteEndpointDataSource.get_Endpoints()
         at Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.CreateEndpointsUnsynchronized()
         at Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.EnsureEndpointsInitialized()
         at Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.get_Endpoints()
         at Microsoft.AspNetCore.Routing.DataSourceDependentCache`1.Initialize()
         at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory)
         at Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher..ctor(EndpointDataSource dataSource, Lifetime lifetime, Func`1 matcherBuilderFactory)
         at Microsoft.AspNetCore.Routing.Matching.DfaMatcherFactory.CreateMatcher(EndpointDataSource dataSource)
         at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.InitializeCoreAsync()
      --- End of stack trace from previous location ---
         at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.<Invoke>g__AwaitMatcher|10_0(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task`1 matcherTask)
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

.NET Version

9.0.301

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions