aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

[NativeAOT] Publishing with NativeAOT and trimming results in broken code

Open AlexMacocian opened this issue 1 year ago • 1 comments

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 out UseRoutes2(app)
    • Build and run the docker image
    • Do an empty POST request to http://localhost/api/oauth/token or a GET request to http://localhost/api/oauth/.well-known/jwks.json
    • Observe the serialized Task<IResult> response
  • Go back to Program.cs

    • Comment .UseRoutes() and uncomment UseRoutes2(app)
    • Build and run the docker image
    • Do an empty POST request to http://localhost/api/oauth/token or a GET request to http://localhost/api/oauth/.well-known/jwks.json
    • Observe the proper json responses
  • Finally, compare generated UseRoutes() extension method with UseRoutes2() method that is copied in Program.cs. UseRoutes2() is a copy of UseRoutes(), 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

AlexMacocian avatar Oct 18 '24 18:10 AlexMacocian

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

AlexMacocian avatar Oct 18 '24 18:10 AlexMacocian

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?

AlexMacocian avatar May 22 '25 11:05 AlexMacocian

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.

AlexMacocian avatar May 22 '25 12:05 AlexMacocian