[NativeAOT] Publishing with NativeAOT and trimming results in broken code
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
### 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 object instead of the IResult result from the Task
### Expected Behavior
```C#
app.MapPost("route", async (someparams) =>
{
return await someMethod();
});
```
should return a json generated by `someMethod()`, not a json that looks like
```json
{
"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 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_
Contributor guide
Assessment
This issue has not been assessed yet.