dotnet / dotnet/aspnetcore

Make Source Generator for UseMiddleware

Open
#45,528 5 comments 1 reaction 0 assignees View on GitHub
area-middleware
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

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

```C#
var app = new ApplicationBuilder(serviceProvider: null);

UseMiddlewareExtensions.UseMiddleware(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:

https://github.com/dotnet/aspnetcore/blob/f543e3552514c5c420eeddd55c505bbc131f10a6/src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs#L62

https://github.com/dotnet/aspnetcore/blob/f543e3552514c5c420eeddd55c505bbc131f10a6/src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs#L113

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.