AOT & trimming
View as MarkdownTenantry builds with the trim and AOT analyzers on. Support differs by package.
Summary
| Package | IsTrimmable | IsAotCompatible | Native AOT | Notes |
|---|---|---|---|---|
Tenantry.Core | ✅ | ✅ | ✅ | No reflection beyond annotated, AOT-safe DI patterns. |
Tenantry.AspNetCore | ✅ | ✅ | ✅ | Demonstrated by the Aot sample. |
Tenantry.Http | ✅ | ✅ | ✅ | Demonstrated by the Aot sample, and checked by a Native AOT build in CI. |
Tenantry.Caching | ✅ | ✅ | ✅ | Checked by a Native AOT build in CI. Microsoft's HybridCache implementation has trim warnings of its own (its default JSON serializer). |
Tenantry.Options | ✅ | ✅ | ✅ | Checked by a Native AOT build in CI. Binding options to configuration (Configure<T>(section)) has its own trimming rules: use the configuration binding source generator. |
Tenantry.EfCore | ✅ | — | ⚠️ Not supported | Its EF Core entry points are annotated, as EF Core's are. |
"Trimmable" means the package raises no trim warnings of its own. "AOT-compatible" means the same for Native AOT.
Tenantry.Core, Tenantry.AspNetCore, Tenantry.Http, Tenantry.Caching and Tenantry.Options
Each is marked IsAotCompatible and IsTrimmable and builds with no warnings under both analyzers. Where the API
takes a type that DI must construct, it is annotated so the trimmer keeps its constructors:
ITenantBuilder<TKey> UseStore<[DynamicallyAccessedMembers(PublicConstructors)] TStore>()
where TStore : class, ITenantStore<TKey>;The Tenantry.Samples.Aot project is a complete ASP.NET Core app
published with Native AOT:
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>It uses WebApplication.CreateSlimBuilder, header-based resolution, the in-memory store, and source-generated
JSON (JsonSerializerContext). ASP.NET Core needs a JsonSerializerContext for the types your own AOT app
serialises too.
dotnet publish samples/Tenantry.Samples.Aot -c ReleaseTenantry.EfCore: trimmable, not AOT-compatible
Tenantry.EfCore is marked IsTrimmable but not IsAotCompatible. UseTenantry() builds its query filters as
expression trees while EF Core builds the model at run time, which Native AOT cannot do. EF Core itself does not
support Native AOT with a model built at run time.
Every public method that configures or creates an EF Core context or model carries [RequiresUnreferencedCode] and
[RequiresDynamicCode], as EF Core's DbContext does: UseTenantry(), AddDbContextPerTenantDatabase and
IsSharedAcrossTenants(). The analyzers warn where your code calls them. Types that only read a model or check a
context, such as TenantModel and TenantContextGuard, are not annotated.
An app that uses Tenantry.EfCore cannot publish with Native AOT.
Recommendations
- For an AOT web app, use
Tenantry.CoreandTenantry.AspNetCore, with a store over a client that supports AOT (a hand-writtenITenantStore, say). - Publish an EF Core app without Native AOT. Trimming works, but test the trimmed build: EF Core providers and your model may need trim roots, as EF Core's guidance describes.