Guides
Trimming & NativeAOT
How AutoSDK generates trimming-compatible, NativeAOT-ready code.
AutoSDK generates code that is fully compatible with .NET trimming and NativeAOT compilation. No runtime reflection is used — all JSON serialization is source-generated.
CLI (Default — No Extra Steps)
The CLI generates trimming-compatible code by default. Generated code uses JsonSerializerContext with source-generated serializers:
autosdk generate openapi.json \
--namespace MyCompany.MyApi \
--output GeneratedThe generated code includes:
SourceGenerationContextclass with[JsonSerializable]attributes for all types- No
System.Reflectionusage - All converters are compile-time generated
Source Generator (Two-Project Pattern)
When using the NuGet source generator, you need a two-project pattern because Roslyn source generators cannot chain — the JSON serializer context must be in a separate compilation unit.
Project 1: Models (generates types + context registrations)
<!-- MyApi.Models.csproj -->
<PropertyGroup>
<AutoSDK_GenerateModels>true</AutoSDK_GenerateModels>
<AutoSDK_GenerateJsonSerializerContextTypes>true</AutoSDK_GenerateJsonSerializerContextTypes>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoSDK" Version="*-*" PrivateAssets="all" />
<AdditionalFiles Include="openapi.yaml" />
</ItemGroup>Project 2: Client (references models, adds JsonSerializerContext)
<!-- MyApi.csproj -->
<PropertyGroup>
<AutoSDK_GenerateMethods>true</AutoSDK_GenerateMethods>
<AutoSDK_GenerateConstructors>true</AutoSDK_GenerateConstructors>
<AutoSDK_JsonSerializerContext>MyCompany.MyApi.SourceGenerationContext</AutoSDK_JsonSerializerContext>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../MyApi.Models/MyApi.Models.csproj" />
<PackageReference Include="AutoSDK" Version="*-*" PrivateAssets="all" />
<AdditionalFiles Include="openapi.yaml" />
</ItemGroup>Add the context class manually:
// SourceGenerationContext.cs
using System.Text.Json.Serialization;
namespace MyCompany.MyApi;
[JsonSerializable(typeof(JsonSerializerContextTypes))]
public partial class SourceGenerationContext : JsonSerializerContext;Validating Trimming
Use the AutoSDK CLI to validate trimming compatibility:
autosdk trim src/libs/MyApi/MyApi.csprojThis builds with trimming enabled and reports any warnings.
Publishing as Single File / NativeAOT
<PropertyGroup>
<PublishAot>true</PublishAot>
<!-- or -->
<PublishSingleFile>true</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>Build with:
dotnet publish -c ReleaseEdit on GitHub
Last updated on