Skip to content

Make Source Generator for UseMiddleware #45528

@eerhardt

Description

@eerhardt

When I dotnet publish -c Release -p:PublishAot=true the following app:

var app = new ApplicationBuilder(serviceProvider: null);

UseMiddlewareExtensions.UseMiddleware<HiMiddleware>(app);

public class HiMiddleware
{
    public Task Invoke(HttpContext context)
    {
        Console.WriteLine("hi");
        return Task.CompletedTask;
    }
}

We should create a Source Generator for UseMiddleware so we don't need to use Reflection on NativeAOT. See #45890

Original issue (outdated)

I get the following AOT warnings stemming from the UseMiddleware method:

/_/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/ArrayOperations.cs(23): AOT analysis warning IL3050: System.Linq.Expressions.Interpreter.NewArrayInitInstruction.Run(InterpretedFrame): Using member 'System.Array.CreateInstance(Type,Int32)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. [C:\DotNetTest\AspNetTest\AspNetTest.csproj]
/_/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/ArrayOperations.cs(52): AOT analysis warning IL3050: System.Linq.Expressions.Interpreter.NewArrayInstruction.Run(InterpretedFrame): Using member 'System.Array.CreateInstance(Type,Int32)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. [C:\DotNetTest\AspNetTest\AspNetTest.csproj]
/_/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/ArrayOperations.cs(87): AOT analysis warning IL3050: System.Linq.Expressions.Interpreter.NewArrayBoundsInstruction.Run(InterpretedFrame): Using member 'System.Array.CreateInstance(Type,Int32[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. [C:\DotNetTest\AspNetTest\AspNetTest.csproj]
/_/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/TypeUtils.cs(28): AOT analysis warning IL3050: System.Dynamic.Utils.TypeUtils.GetNullableType(Type): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [C:\DotNetTest\AspNetTest\AspNetTest.csproj]
/_/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/MethodCallExpression.cs(1379): AOT analysis warning IL3050: System.Linq.Expressions.Expression.ApplyTypeArgs(MethodInfo,Type[]): Using member 'System.Reflection.MethodInfo.MakeGenericMethod(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [C:\DotNetTest\AspNetTest\AspNetTest.csproj]
/_/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/BinaryExpression.cs(2239): AOT analysis warning IL3050: System.Linq.Expressions.Expression.GetResultTypeOfShift(Type,Type): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [C:\DotNetTest\AspNetTest\AspNetTest.csproj]

This causes AOT warnings for ASP.NET apps that want to use EndPoints and Routing because we call UseMiddleware to enable these features:

return builder.UseMiddleware<EndpointRoutingMiddleware>(endpointRouteBuilder);

return builder.UseMiddleware<EndpointMiddleware>();

At a minimum, we should remove the AOT warnings from the above 2 callsites, so we can publish an ASP.NET Web API application for NativeAOT with no warnings.

As a follow up after that, we should consider a mechanism for solving this warning generally, for example with a source generator that generates the necessary code at compile-time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewares

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions