![]() |
VOOZH | about |
dotnet add package ZeroAlloc.Inject.Generator --version 1.7.1
NuGet\Install-Package ZeroAlloc.Inject.Generator -Version 1.7.1
<PackageReference Include="ZeroAlloc.Inject.Generator" Version="1.7.1" />
<PackageVersion Include="ZeroAlloc.Inject.Generator" Version="1.7.1" />Directory.Packages.props
<PackageReference Include="ZeroAlloc.Inject.Generator" />Project file
paket add ZeroAlloc.Inject.Generator --version 1.7.1
#r "nuget: ZeroAlloc.Inject.Generator, 1.7.1"
#:package ZeroAlloc.Inject.Generator@1.7.1
#addin nuget:?package=ZeroAlloc.Inject.Generator&version=1.7.1Install as a Cake Addin
#tool nuget:?package=ZeroAlloc.Inject.Generator&version=1.7.1Install as a Cake Tool
๐ NuGet
๐ Build
๐ AOT
๐ GitHub Sponsors
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.
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.
// 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")].
.NET 10 host / .NET 9.0.15 runtime, BenchmarkDotNet v0.15.8, Intel Core i9-12900HK. Compared against MS DI (reflection) and Jab (the other source-gen DI library).
| Scenario | MS DI | ZA Container | ZA Standalone | Jab |
|---|---|---|---|---|
| Startup / registration | 6,413 ns / 6,944 B | 1,034 ns / 2,664 B | 8 ns / 48 B | 8 ns / 40 B |
| Transient (1 dep) | 31 ns | 27 ns | 24 ns | 47 ns |
| Transient (1 property dep) | 44 ns | 27 ns | 22 ns | N/Aยน |
| Decorated transient | 44 ns | 21 ns | 22 ns | 29 nsยฒ |
IEnumerable<T> (3 impls) |
68 ns | 75 ns | 82 ns | 151 ns |
| Open generic (closed type) | 14 ns | (via MS DI) | 8 ns | N/Aยณ |
| Create scope | 82 ns / 128 B | 60 ns / 96 B | 58 ns / 88 B | 14 ns / 40 B |
| Resolve scoped (full lifecycle) | 7,181 ns / 304 B | 5,901 ns / 120 B | 4,851 ns / 120 B | 5,216 ns / 120 B |
ยน Jab is constructor-only. ยฒ Jab decorator wired via factory. ยณ Jab 0.10.x requires closed types.
ZA wins where the generator's domain knowledge matters: property injection (2ร MS DI), decorators (2.1ร MS DI), open generics (1.8ร MS DI). Jab wins on scope creation and pure scoped lifecycle. MS DI wins on IEnumerable<T> (its cached multi-registration enumeration is excellent).
In v1.6 the ZA Container scope creation dropped 50% on allocation (216 B โ 96 B) and 2ร faster โ the MS DI fallback scope is no longer eagerly created; it materializes on first fallback resolution. For ZA-owned registrations (the common case), it never materializes at all. The IEnumerable row above shows the all-Transient registration path; all-Singleton IEnumerable<T> resolutions are now cached (0 B on subsequent calls โ verified by tests).
Full methodology, all scenarios, and analysis: docs/performance.md.
[Transient], [Scoped], [Singleton] attributes; the generator emits all IServiceCollection.Add* callsnew ClassName(...) constructor calls and typeof(T) type switches[Decorator] / [DecoratorOf] with ordering, conditional application (WhenRegistered), and optional dependenciesKey = "redis" on any lifetime attribute (.NET 8+)TryAdd by default โ prevents duplicate registrations; opt in to AllowMultiple = true for IHostedService scenarios| 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 |
| 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 |
MIT
Learn more about Target Frameworks and .NET Standard.
Showing the top 1 NuGet packages that depend on ZeroAlloc.Inject.Generator:
| Package | Downloads |
|---|---|
|
AI.Sentinel
Security monitoring middleware for IChatClient โ prompt injection, hallucination, and operational anomaly detection with an intervention engine. |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.7.1 | 1,474 | 5/28/2026 |
| 1.7.0 | 681 | 5/18/2026 |
| 1.6.0 | 196 | 5/18/2026 |
| 1.5.0 | 440 | 5/12/2026 |
| 1.4.2 | 280 | 5/12/2026 |
| 1.4.1 | 302 | 5/3/2026 |
| 1.4.0 | 781 | 5/1/2026 |
| 1.3.2 | 837 | 4/29/2026 |
| 1.3.1 | 479 | 4/29/2026 |
| 1.3.0 | 446 | 4/25/2026 |
| 1.2.2 | 111 | 3/20/2026 |
| 1.2.1 | 104 | 3/19/2026 |
| 1.2.0 | 105 | 3/17/2026 |
| 1.1.0 | 108 | 3/16/2026 |
| 0.11.3 | 107 | 3/15/2026 |