wshaddix/dotnet-skills/dotnet-aot-architecture
Claude Code and agent skill by wshaddix/dotnet-skills
- community
Checking that the SKILL.md file is still in the repository.
Designing AOT-first apps. Source gen over reflection, AOT-safe DI, serialization, factories.
Install
npx vskill@latest install wshaddix/dotnet-skills/dotnet-aot-architectureSKILL.md
dotnet-aot-architecture
AOT-first application design patterns for .NET 8+: preferring source generators over reflection, explicit DI registration over assembly scanning, AOT-safe serialization choices, library compatibility assessment, and factory patterns replacing Activator.CreateInstance.
Version assumptions: .NET 8.0+ baseline. Patterns apply to all AOT-capable project types (console, ASP.NET Core Minimal APIs, worker services).
Out of scope: Native AOT publish pipeline and MSBuild configuration -- see [skill:dotnet-native-aot]. Trim-safe library authoring and annotations -- see [skill:dotnet-trimming]. WASM AOT compilation -- see [skill:dotnet-aot-wasm]. MAUI-specific AOT -- see [skill:dotnet-maui-aot]. Source generator authoring (Roslyn API) -- see [skill:dotnet-csharp-source-generators]. DI container internals -- see [skill:dotnet-csharp-dependency-injection]. Serialization depth -- see [skill:dotnet-serialization].
Cross-references: [skill:dotnet-native-aot] for the AOT publish pipeline, [skill:dotnet-trimming] for trim annotations and library authoring, [skill:dotnet-serialization] for serialization patterns, [skill:dotnet-csharp-source-generators] for source gen mechanics, [skill:dotnet-csharp-dependency-injection] for DI fundamentals, [skill:dotnet-containers] for runtime-deps deployment, [skill:dotnet-native-interop] for general P/Invoke patterns and marshalling.
Source Generators Over Reflection
The primary AOT enabler is replacing runtime reflection with compile-time source generation. Source generators produce code at build time that the AOT compiler can analyze and include.
Key Source Generator Replacements
| Reflection Pattern | Source Generator / AOT-Safe Alternative | Library | |
|---|---|---|---|
JsonSerializer.Deserialize<T>() | [JsonSerializable] context | System.Text.Json (built-in) | |
Activator.CreateInstance<T>() | Factory pattern with explicit new | Manual | |
Type.GetProperties() for mapping | [Mapper] attribute | Mapperly | |
Regex pattern compilation | [GeneratedRegex] attribute | Built-in (.NET 7+) | |
ILogger.Log(...) with string interpolation | [LoggerMessage] attribute | Microsoft.Extensions.Logging | |
| Assembly scanning for DI | Explicit services.Add*() | Manual | |
[DllImport] P/Invoke | [LibraryImport] | Built-in (.NET 7+) | |
AutoMapper CreateMap<>() | [Mapper] source gen | Mapperly |
Example: Migrating to Source Gen
See [skill:dotnet-csharp-source-generators] for source generator mechanics and authoring patterns.
AOT-Safe DI Patterns
Dependency injection in AOT requires explicit service registration. Assembly scanning (AddServicesFromAssembly) and open-generic resolution may require reflection that AOT cannot satisfy.
Explicit Registration (Preferred)
Avoid Assembly Scanning
Keyed Services (.NET 8+)
See [skill:dotnet-csharp-dependency-injection] for full DI patterns.
Serialization Choices for AOT
Decision Matrix
| Serializer | AOT-Safe | Setup Required | Best For | |
|---|---|---|---|---|
| System.Text.Json + source gen | Yes | [JsonSerializable] context | APIs, config, JSON interop | |
| Protobuf (Google.Protobuf) | Yes | .proto schema files | gRPC, service-to-service | |
| MessagePack + source gen | Yes | [MessagePackObject] + source gen resolver | Caching, real-time | |
| Newtonsoft.Json | No | N/A | Do not use for AOT | |
| STJ without source gen | No | N/A | Falls back to reflection |
STJ Source Gen Setup
See [skill:dotnet-serialization] for comprehensive serialization patterns.
Factory Patterns Replacing Activator.CreateInstance
Activator.CreateInstance uses runtime reflection to create instances and is incompatible with AOT. Replace with factory patterns that use explicit construction.
Simple Factory
Strategy Pattern via DI
Enum-Based Factory
Library Compatibility Assessment
Assessment Checklist
Before adopting a NuGet package in an AOT project:
- Check for `IsAotCompatible` in the package source -- packages that set this are validated against AOT analyzers
- Check for `[RequiresDynamicCode]` / `[RequiresUnreferencedCode]` annotations -- these indicate AOT-incompatible APIs
- Run AOT analyzers against your usage --
dotnet build /p:EnableAotAnalyzer=true - Check the package's GitHub issues for AOT/trimming reports -- search for "Native AOT", "trimming", "IL2026", "IL3050"
- Look for source-generated alternatives -- many reflection-based libraries now have source-gen companions
Common Library Status
| Library | AOT Status | AOT-Safe Alternative | |
|---|---|---|---|
| AutoMapper | Breaks | Mapperly | |
| MediatR | Partial (explicit registration) | Direct method calls or factory | |
| FluentValidation | Partial | Manual validation or source gen | |
| Dapper | Compatible (.NET 8+ AOT support) | -- | |
| Entity Framework Core | Partial (precompiled queries) | Dapper for AOT-heavy paths | |
| Refit | Compatible (7+ with source gen) | -- | |
| Polly | Compatible (v8+) | -- | |
| Serilog | Partial | [LoggerMessage] source gen | |
| Hangfire | Breaks | Custom IHostedService |
Testing Compatibility
AOT Application Architecture Template
Agent Gotchas
- Do not use `Activator.CreateInstance` in AOT projects. It requires runtime reflection that is not available. Use factory patterns, DI keyed services, or switch expressions instead.
- Do not use assembly scanning for DI registration (
Scan,RegisterAssemblyTypes,FromAssemblyOf). These use reflection to discover types at runtime. Register services explicitly. - Do not use `System.Text.Json` without a `[JsonSerializable]` context in AOT. Without a source-generated context, STJ falls back to reflection and fails at runtime.
- Do not assume a library is AOT-compatible without testing. Run
dotnet build /p:EnableAotAnalyzer=trueand check for IL3050/IL2026 warnings against your specific usage. - Do not use `Type.GetType()` or `Assembly.GetTypes()` for runtime discovery. These rely on metadata that may be trimmed. Use compile-time known types.
References
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/dotnet-aot-architecture/SKILL.md
Popularity
- Repository stars
- 86
- Repository forks
- 14
- 7-day trend score
- 3
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 18 Mar 2026, 7 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/wshaddix/dotnet-skills/dotnet-aot-architecture)