Skip to content

RequestDelegateMetadataResult.CachedFactoryContext is unused, resulting in unnecessary work  #45840

@andrewlock

Description

@andrewlock

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I was looking through some of the code related to RequestDelegateFactory and I noticed that RequestDelegateMetadataResult.CachedFactoryContext doesn't appear to be used. I'm not sure whether this is a bug or by design, but it seems like not using it results in a lot of duplicated work building Expressions? Especially as the documentation for CachedFactoryContext says:

// This internal cached context avoids redoing unnecessary reflection in Create that was already done in InferMetadata.
// InferMetadata currently does more work than it needs to building up expression trees, but the expectation is that InferMetadata will usually be followed by Create.
internal RequestDelegateFactoryContext? CachedFactoryContext { get; set; }

Expected Behavior

I would expect the CachedFactoryContext to be set in InferMetadata, e.g something like this:

public static RequestDelegateMetadataResult InferMetadata(MethodInfo methodInfo, RequestDelegateFactoryOptions? options = null)
{
    var factoryContext = CreateFactoryContext(options);
    factoryContext.ArgumentExpressions = CreateArgumentsAndInferMetadata(methodInfo, factoryContext);

    return new RequestDelegateMetadataResult
    {
        EndpointMetadata = AsReadOnlyList(factoryContext.EndpointBuilder.Metadata),
+       CachedFactoryContext = factoryContext,
    };
}

Steps To Reproduce

I simply created a new minimal API app, debugged into RequestDelegateFactory, and placed a breakpoint in CreateArguments(). It's called both in InferMetadata() and Create(), which CachedFactoryContext is meant to avoid (if I understand correctly!)

Exceptions (if any)

No response

.NET Version

7.0.101

Anything else?

No response

Metadata

Metadata

Assignees

Labels

old-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions