The OpenFeature .NET SDK is compatible with .NET NativeAOT compilation, allowing you to create self-contained, native executables with faster startup times and lower memory usage.
Fully Compatible - The SDK can be used in NativeAOT applications without any issues.
- Core API functionality (
Api.Instance,GetClient(), flag evaluations) - All built-in providers (
NoOpProvider, etc.) - JSON serialization of
Value,Structure, andEvaluationContext - Error handling and enum descriptions
- Hook system
- Event handling
- Metrics collection
- Dependency injection
To enable NativeAOT in your project, add these properties to your .csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <!-- or net9.0 -->
<OutputType>Exe</OutputType>
<!-- Enable NativeAOT -->
<PublishAot>true</PublishAot>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenFeature" Version="2.x.x" />
</ItemGroup>
</Project>using OpenFeature;
using OpenFeature.Model;
// Basic OpenFeature usage - fully AOT compatible
var api = Api.Instance;
var client = api.GetClient("my-app");
// All flag evaluation methods work
var boolFlag = await client.GetBooleanValueAsync("feature-enabled", false);
var stringFlag = await client.GetStringValueAsync("welcome-message", "Hello");
var intFlag = await client.GetIntegerValueAsync("max-items", 10);For optimal AOT performance, use the provided JsonSerializerContext:
using System.Text.Json;
using OpenFeature.Model;
using OpenFeature.Serialization;
var value = new Value(Structure.Builder()
.Set("name", "test")
.Set("enabled", true)
.Build());
// Use AOT-compatible serialization
var json = JsonSerializer.Serialize(value, OpenFeatureJsonSerializerContext.Default.Value);
var deserialized = JsonSerializer.Deserialize(json, OpenFeatureJsonSerializerContext.Default.Value);Build and publish your AOT application:
# Build with AOT analysis
dotnet build -c Release
# Publish as native executable
dotnet publish -c Release
# Run the native executable (example path for macOS ARM64)
./bin/Release/net9.0/osx-arm64/publish/MyAppNativeAOT compilation provides several benefits:
- Faster Startup: Native executables start faster than JIT-compiled applications
- Lower Memory Usage: Reduced memory footprint
- Self-Contained: No .NET runtime dependency required
- Smaller Deployment: Optimized for size with trimming
The SDK includes an AOT compatibility test project at test/OpenFeature.AotCompatibility/ that:
- Tests all core SDK functionality
- Validates JSON serialization with source generation
- Verifies error handling works correctly
- Can be compiled and run as a native executable
Run the test:
cd test/OpenFeature.AotCompatibility
dotnet publish -c Release
./bin/Release/net9.0/[runtime]/publish/OpenFeature.AotCompatibilityCurrently, there are no known limitations when using OpenFeature with NativeAOT. All core functionality is fully supported.
When using third-party providers, ensure they are also AOT-compatible. Check the provider's documentation for AOT support.
If you encounter trimming warnings, you can:
- Use the provided
JsonSerializerContextfor JSON operations - Ensure your providers are AOT-compatible
- Add appropriate
[DynamicallyAccessedMembers]attributes if needed
- Ensure you're targeting .NET 8.0 or later
- Verify all dependencies support NativeAOT
- Check that
PublishAotis set totrue
If migrating from a non-AOT setup:
- JSON Serialization: Replace direct
JsonSerializercalls with the provided context - Reflection: The SDK no longer uses reflection, but ensure your custom code doesn't
- Dynamic Loading: Avoid dynamic assembly loading; register providers at compile time
See the complete example in test/OpenFeature.AotCompatibility/Program.cs for a working AOT application that demonstrates all SDK features.