Make projects AOT compatible#10006
Merged
NikolaMilosavljevic merged 1 commit intodotnet:mainfrom Mar 23, 2026
Merged
Conversation
MiYanni
approved these changes
Mar 20, 2026
mmitche
pushed a commit
to dotnet/sdk
that referenced
this pull request
Apr 2, 2026
Contributes to dotnet/templating#9193 Summary of changes. ## 1. Enable AOT compatibility analyzers on all `src/` projects Each `.csproj` under `src/` received: ```xml <IsAotCompatible Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETCoreApp'">true</IsAotCompatible> ``` The condition restricts it to .NET Core TFMs, avoiding errors on `netstandard2.0` / `net472`. ## 2. Replace reflection-based JSON serialization with source-generated contexts Three new `JsonSerializerContext` files were added to eliminate AOT-unsafe `JsonSerializer.Serialize(obj)` calls: | New file | Serializable type | |---|---| | `GlobalSettingsJsonSerializerContext.cs` | `GlobalSettingsData` | | `SettingsStoreJsonSerializerContext.cs` | `SettingsStore` | | `TemplateCacheJsonSerializerContext.cs` | `TemplateCache` | The callers in `GlobalSettings.cs`, `ComponentManager.cs`, and `TemplatePackageManager.cs` were updated to use the new typed `WriteObject<T>` overload with `JsonTypeInfo<T>`. ## 3. Convert `WriteObject` to a type-safe generic In `JExtensions.cs`, the untyped `WriteObject(string, object)` was replaced with: ```csharp WriteObject<T>(string path, T obj, JsonTypeInfo<T> jsonTypeInfo) ``` This uses `JsonSerializer.Serialize(stream, obj, jsonTypeInfo)` — fully AOT-safe. ## 4. Source-gen context for `JsonEncodeValueFormFactory` In `JsonEncodeValueFormFactory.cs`, the static `JsonSerializerOptions` + `JsonSerializer.Serialize(value, options)` was replaced with a `JsonEncodeSerializerContext` using `[JsonSerializable(typeof(string))]`, calling `JsonSerializer.Serialize(value, context.String)`. ## 5. AOT suppression attributes on reflection-heavy methods Methods that inherently use reflection (assembly loading, `Type.GetType()`, `Activator.CreateInstance`) received method-level `[UnconditionalSuppressMessage]` attributes with specific IL warning codes: | File | Methods annotated | Warning codes | |---|---|---| | `ComponentManager.cs` | Constructor, `TryGetComponent<T>`, `RegisterType`, `GetType` | IL2026, IL2057, IL2067, IL2070, IL2072, IL3000, IL3050 | | `Scanner.cs` | `ScanForComponents`, `LoadAllFromPath` | IL2026 | | `ReflectionLoadProbingPath.cs` | `SelectBestMatch`, `Resolving` | IL2026 | | `Bootstrapper.cs` | `Register(Assembly)` | IL2026 | ## 6. AOT annotations on remaining unsafe shared methods `ToJsonString(object)` and `FromObject(object)` in `JExtensions.cs` received `[RequiresUnreferencedCode]` + `[RequiresDynamicCode]` attributes, propagating warnings to callers. The callers in `TemplateSearchCache.Json.cs`, `TemplateSearchData.Json.cs`, `TemplatePackageSearchData.Json.cs`, and `TemplateDiscoveryMetadata.cs` received corresponding `[UnconditionalSuppressMessage]` attributes with justifications. All attributes are guarded with `#if NET7_0_OR_GREATER` (or `#if NET` where appropriate) so they compile cleanly on older TFMs. Commit migrated from dotnet/templating@97a3783
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contributes to #9193
Summary of changes.
1. Enable AOT compatibility analyzers on all
src/projectsEach
.csprojundersrc/received:The condition restricts it to .NET Core TFMs, avoiding errors on
netstandard2.0/net472.2. Replace reflection-based JSON serialization with source-generated contexts
Three new
JsonSerializerContextfiles were added to eliminate AOT-unsafeJsonSerializer.Serialize(obj)calls:GlobalSettingsJsonSerializerContext.csGlobalSettingsDataSettingsStoreJsonSerializerContext.csSettingsStoreTemplateCacheJsonSerializerContext.csTemplateCacheThe callers in
GlobalSettings.cs,ComponentManager.cs, andTemplatePackageManager.cswere updated to use the new typedWriteObject<T>overload withJsonTypeInfo<T>.3. Convert
WriteObjectto a type-safe genericIn
JExtensions.cs, the untypedWriteObject(string, object)was replaced with:This uses
JsonSerializer.Serialize(stream, obj, jsonTypeInfo)— fully AOT-safe.4. Source-gen context for
JsonEncodeValueFormFactoryIn
JsonEncodeValueFormFactory.cs, the staticJsonSerializerOptions+JsonSerializer.Serialize(value, options)was replaced with aJsonEncodeSerializerContextusing[JsonSerializable(typeof(string))], callingJsonSerializer.Serialize(value, context.String).5. AOT suppression attributes on reflection-heavy methods
Methods that inherently use reflection (assembly loading,
Type.GetType(),Activator.CreateInstance) received method-level[UnconditionalSuppressMessage]attributes with specific IL warning codes:ComponentManager.csTryGetComponent<T>,RegisterType,GetTypeScanner.csScanForComponents,LoadAllFromPathReflectionLoadProbingPath.csSelectBestMatch,ResolvingBootstrapper.csRegister(Assembly)6. AOT annotations on remaining unsafe shared methods
ToJsonString(object)andFromObject(object)inJExtensions.csreceived[RequiresUnreferencedCode]+[RequiresDynamicCode]attributes, propagating warnings to callers. The callers inTemplateSearchCache.Json.cs,TemplateSearchData.Json.cs,TemplatePackageSearchData.Json.cs, andTemplateDiscoveryMetadata.csreceived corresponding[UnconditionalSuppressMessage]attributes with justifications.All attributes are guarded with
#if NET7_0_OR_GREATER(or#if NETwhere appropriate) so they compile cleanly on older TFMs.