Skip to content

Add attributes for NativeAOT safety - #1196

Merged
Andrew Arnott (AArnott) merged 23 commits into
mainfrom
NativeAOT
Jun 19, 2025
Merged

Add attributes for NativeAOT safety#1196
Andrew Arnott (AArnott) merged 23 commits into
mainfrom
NativeAOT

Conversation

@AArnott

Copy link
Copy Markdown
Member

This doesn't get StreamJsonRpc to be NativeAOT safe in any significant way, but now attributes guard the areas that are not safe, and gives us a backlog of things to improve on.

Since ProxyGeneration always generates IL code using RefEmit, it will never work in native AOT'd apps. But currently it is still brought into the app because RpcMarshalableConverterFactory (used by SystemTextJsonFormatter by default) eventually calls into it.
To remove references to Newtonsoft.Json, we can cut this off in AOT'd apps by checking if dynamic code isn't supported and throw an exception. This will allow ProxyGeneration to be trimmed from the final app.
This isn't a perfect fix, but instead a compromise. The best fix would be to make RpcMarshalableConverterFactory work correctly in AOT'd apps.
Cut ProxyGeneration out if dynamic code isn't supported
David Fowler (davidfowl) pushed a commit to microsoft/aspire that referenced this pull request Jun 17, 2025
* Start the beginning of AOT'ing the Aspire.Cli.
Depends on microsoft/vs-streamjsonrpc#1196
* Revert cli.csproj changes
* Revert Designer.cs changes
* Share state object types across CLI and Hosting.
* Split the Backchannel data types across assemblies again
* Revert launchSettings.json
…bility.
There are a couple patterns here:
- Use GetMemberWithSameMetadataDefinitionAs to reflect on generic types/methods in a statically verifiable fashion. This method only exists in .NET 6+.
- Suppress warnings when using GetInterfaces when the Type is annotated as preserve 'All'. dotnet/linker#1731
- When looking for a hard-coded interface like IProgress<T>, any preserved Type that implements the interface won't have the interface trimmed.
- MethodNameMap uses GetInterfaceMap, which may not work in all cases. See dotnet/runtime#89157
- Use for loop to avoid Roslyn analyzer warning.
- Condition the suppression on !NET10.
…bility. (#1207)
* Refactor code and annotations to fix all warnings in NativeAOTCompatibility.
There are a couple patterns here:
- Use GetMemberWithSameMetadataDefinitionAs to reflect on generic types/methods in a statically verifiable fashion. This method only exists in .NET 6+.
- Suppress warnings when using GetInterfaces when the Type is annotated as preserve 'All'. dotnet/linker#1731
- When looking for a hard-coded interface like IProgress<T>, any preserved Type that implements the interface won't have the interface trimmed.
- MethodNameMap uses GetInterfaceMap, which may not work in all cases. See dotnet/runtime#89157
* Refactor TrackerHelper to not need DynamicallyAccessedMembers attribute on the Type parameter.
* Respond to PR feedback
- Use for loop to avoid Roslyn analyzer warning.
- Condition the suppression on !NET10.
- Remove leftover comment
- Fix some incorrect trimming annotations

@eerhardtEric Erhardt (eerhardt) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes LGTM.

I found a few minor pieces of feedback which I addressed in #1210.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@AArnott@eerhardt@matteo-prosperi