aspnetcore
aspnetcore copied to clipboard
[NativeAOT] Publishing with NativeAOT and trimming results in broken code
Is there an existing issue for this?
- [x] I have searched the existing issues
Describe the bug
I have a source generator that builds some minimal api from controller classes. The source generator creates an extension method for WebApplication which maps all methods from the controllers with the dependencies and parameters of the methods.
When running under Debug, the app works fine and returns expected responses. When building my docker image and publishing the app with NativeAOT and trimming, on certain flows, the API calls return a serialization of the Task<IResult> object instead of the IResult result from the Task<IResult>
Expected Behavior
app.MapPost("route", async (someparams) =>
{
return await someMethod();
});
should return a json generated by someMethod(), not a json that looks like
{
"result": {},
"asyncState": null,
"creationOptions": 0,
"exception": null,
"id": 2,
"isCanceled": false,
"isCompleted": true,
"isCompletedSuccessfully": true,
"isFaulted": false,
"status": 5
}
Steps To Reproduce
Here's a link to the repo:
https://github.com/AlexMacocian/Badge
Run docker compose up in Badge subdirectory.
-
In
Program.cs:- Uncomment
.UseRoutes()and comment outUseRoutes2(app) - Build and run the docker image
- Do an empty POST request to
http://localhost/api/oauth/tokenor a GET request tohttp://localhost/api/oauth/.well-known/jwks.json - Observe the serialized Task<IResult> response
- Uncomment
-
Go back to
Program.cs- Comment
.UseRoutes()and uncommentUseRoutes2(app) - Build and run the docker image
- Do an empty POST request to
http://localhost/api/oauth/tokenor a GET request tohttp://localhost/api/oauth/.well-known/jwks.json - Observe the proper json responses
- Comment
-
Finally, compare generated
UseRoutes()extension method withUseRoutes2()method that is copied inProgram.cs.UseRoutes2()is a copy ofUseRoutes(), just placed outside of the generated extension and not marked as an extension method. But otherwise, it contains the same code.
Exceptions (if any)
No response
.NET Version
.NET SDK: Version: 8.0.403 Commit: c64aa40a71 Workload version: 8.0.400-manifests.18f19b92 MSBuild version: 17.11.9+a69bbaaf5 Runtime Environment: OS Name: debian OS Version: 12 OS Platform: Linux RID: linux-x64 Base Path: /usr/share/dotnet/sdk/8.0.403/ .NET workloads installed: Configured to use loose manifests when installing new manifests. There are no installed workloads to display. Host: Version: 8.0.10 Architecture: x64 Commit: 81cabf2857 .NET SDKs installed: 8.0.403 [/usr/share/dotnet/sdk] .NET runtimes installed: Microsoft.AspNetCore.App 8.0.10 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 8.0.10 [/usr/share/dotnet/shared/Microsoft.NETCore.App] Other architectures found: None Environment variables: Not set global.json file: Not found Learn more: https://aka.ms/dotnet/info Download .NET: https://aka.ms/dotnet/download
Anything else?
No response
Another thing to note, if I copy the generated code from UseRoutes() inside Program.cs, call it UseRoutes2() and call that one instead, I no longer have the issue described above.
This really looks like something that has to do with the generated code is compiled. But it makes no sense that the same code would result in different functionality, if copied inside the Program.cs
Is most probably related to this https://learn.microsoft.com/en-us/aspnet/core/fundamentals/aot/request-delegate-generator/rdg?view=aspnetcore-9.0
But the delegate generator doesn't seem to work for the generated code?
For anybody facing a similar issue, I've fixed this by generating a deferred class of type IResult whose job is to execute the inner task on ExecuteAsync
Solution:
public sealed class DeferredResult : IResult
{
public DeferredResult(Task<IResult> inner)
{
this.inner = inner;
}
public readonly Task<IResult> inner;
public async Task ExecuteAsync(HttpContext httpContext)
{
var result = await this.inner;
await result.ExecuteAsync(httpContext);
}
}
And in my generated extension:
static Microsoft.AspNetCore.Http.IResult TestControllerGetTest2(HttpContext httpContext, TestController route)
{
var cancellationToken = httpContext.RequestAborted;
return new DeferredResult(route.GetTest2(cancellationToken));
}
builder.MapGet("/api/test/game", TestControllerGetTest2);
Reasoning:
It turns out that all of this was due to Request Delegate Generation not generating the proper delegate for my generated code.
This seems to be intentional but I couldn't find any documentation on why. Because the delegate was not being properly generated, Asp would see cast my delegate to object and fallback to json serialization.
By implementing an IResult that wraps around my Task<IResult>, I give Asp a delegate that it can handle, and I do the actual awaiting inside the new IResult type.