ZeroAlloc.Inject.Container
1.2.0
dotnet add package ZeroAlloc.Inject.Container --version 1.2.0
NuGet\Install-Package ZeroAlloc.Inject.Container -Version 1.2.0
<PackageReference Include="ZeroAlloc.Inject.Container" Version="1.2.0" />
<PackageVersion Include="ZeroAlloc.Inject.Container" Version="1.2.0" />
<PackageReference Include="ZeroAlloc.Inject.Container" />
paket add ZeroAlloc.Inject.Container --version 1.2.0
#r "nuget: ZeroAlloc.Inject.Container, 1.2.0"
#:package ZeroAlloc.Inject.Container@1.2.0
#addin nuget:?package=ZeroAlloc.Inject.Container&version=1.2.0
#tool nuget:?package=ZeroAlloc.Inject.Container&version=1.2.0
ZeroAlloc.Inject
Compile-time DI registration for .NET. A Roslyn source generator that auto-discovers services via attributes and generates IServiceCollection extension methods and a Native AOT-compatible IServiceProvider. No reflection, no runtime scanning — misconfigured dependencies surface as build errors before the application ever starts.
Install
For ASP.NET Core and Generic Host projects (MS DI runtime):
dotnet add package ZeroAlloc.Inject
dotnet add package ZeroAlloc.Inject.Generator
For standalone or hybrid container mode (no separate attribute package needed):
dotnet add package ZeroAlloc.Inject.Container
ZeroAlloc.Inject.Container bundles the generator, attributes, and the container base classes in a single package.
30-Second Example
// 1. Annotate your service with a lifetime attribute
using ZeroAlloc.Inject;
[Transient] // registered as IOrderService + OrderService
public class OrderService : IOrderService
{
private readonly IProductRepository _repo;
public OrderService(IProductRepository repo) => _repo = repo;
public Order PlaceOrder(int productId) => /* ... */
}
// 2. Register all attributed services in one generated call
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMyAppServices(); // generated at compile time from assembly name
// 3. Resolve the service — or let the framework inject it for you
var app = builder.Build();
var service = app.Services.GetRequiredService<IOrderService>();
The generator derives the method name from the assembly name: MyApp → AddMyAppServices(), MyApp.Domain → AddMyAppDomainServices(). Override with [assembly: ZeroAllocInject("AddDomainServices")].
Performance
.NET 9.0, BenchmarkDotNet v0.15.8, Windows 11 (Intel Core i9-12900HK), X64 RyuJIT AVX2.
| Scenario | MS DI | Hybrid Container | Standalone |
|---|---|---|---|
| Startup / registration | 139 ns / 528 B | 4,477 ns / 9,368 B | 5 ns / 32 B |
| Transient (no deps) | 23 ns | 13 ns | 15 ns |
| Singleton | 16 ns | 11 ns | 12 ns |
| Decorated transient | 41 ns / 48 B | 18 ns / 48 B | 17 ns / 48 B |
| Transient (1 property-injected dep) | 38 ns / 48 B | 19 ns / 48 B | 22 ns / 48 B |
| Resolve scoped (full lifecycle) | ~26,000 ns / 808 B | ~12,300 ns / 120 B | ~12,000 ns / 120 B |
Full methodology, all scenarios, and analysis: docs/performance.md.
Features
- Compile-time registration —
[Transient],[Scoped],[Singleton]attributes; the generator emits allIServiceCollection.Add*calls - Zero reflection — generated code uses direct
new ClassName(...)constructor calls andtypeof(T)type switches - Native AOT compatible — standalone container is 100% AOT-safe including open generics
- Three container modes — MS DI extension method, Hybrid (wraps MS DI), and Standalone (no runtime MS DI dependency)
- Decorators —
[Decorator]/[DecoratorOf]with ordering, conditional application (WhenRegistered), and optional dependencies - Keyed services —
Key = "redis"on any lifetime attribute (.NET 8+) - Open generics — single attribute covers all closed forms; standalone mode enumerates closed usages at compile time
- Compile-time diagnostics — ZAI001–ZAI018 reported as build errors/warnings, including circular dependency detection
TryAddby default — prevents duplicate registrations; opt in toAllowMultiple = trueforIHostedServicescenarios- Multi-assembly — each assembly generates its own extension method; call them in sequence at the composition root
Documentation
| Page | Description |
|---|---|
| Getting Started | Install, annotate services, call the generated method |
| Service Registration | Lifetime attributes, As, keyed services, open generics |
| Container Modes | MS DI Extension, Hybrid, and Standalone — trade-offs and setup |
| Decorators | [Decorator], [DecoratorOf], ordering, conditional decorators |
| Native AOT | Trimmer safety, publishing, ASP.NET Core AOT setup |
| Advanced Patterns | Multi-assembly, constructor disambiguation, collection injection |
| Compiler Diagnostics | ZAI001–ZAI018 reference with triggers and fixes |
| Performance | Full benchmark tables and analysis |
| Testing | Unit testing without the container, integration test setup per mode |
How It Compares to Scrutor
| ZeroAlloc.Inject | Scrutor | |
|---|---|---|
| Discovery | Compile-time source gen | Runtime assembly scanning |
| Reflection | None | Yes |
| Native AOT | ✅ Standalone mode | ❌ |
| Startup cost | Zero | Scales with assembly size |
| IDE support | Compile errors + warnings | Runtime exceptions |
| Configuration | Attributes on classes | Fluent API in Program.cs |
Requirements
- .NET 8.0, .NET 9.0, or .NET 10.0
- C# 12+
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- ZeroAlloc.Inject (>= 1.2.0)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- ZeroAlloc.Inject (>= 1.2.0)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- ZeroAlloc.Inject (>= 1.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.