ZeroAlloc.Inject
1.11.5
dotnet add package ZeroAlloc.Inject --version 1.11.5
NuGet\Install-Package ZeroAlloc.Inject -Version 1.11.5
<PackageReference Include="ZeroAlloc.Inject" Version="1.11.5" />
<PackageVersion Include="ZeroAlloc.Inject" Version="1.11.5" />
<PackageReference Include="ZeroAlloc.Inject" />
paket add ZeroAlloc.Inject --version 1.11.5
#r "nuget: ZeroAlloc.Inject, 1.11.5"
#:package ZeroAlloc.Inject@1.11.5
#addin nuget:?package=ZeroAlloc.Inject&version=1.11.5
#tool nuget:?package=ZeroAlloc.Inject&version=1.11.5
ZeroAlloc.Inject
Compile-time dependency injection for .NET: put [Transient], [Scoped] or [Singleton] on a class
and the generator writes the registrations, with no reflection, no assembly scanning and NativeAOT-ready.
Why ZeroAlloc.Inject
- Starts almost instantly. Building the standalone provider takes about 1% of the time and 112 B,
against 4,384 B for Microsoft DI's
BuildServiceProvider(). - About 2× faster than Microsoft DI where the generator knows your graph. Decorated services and
property injection resolve in 0.59× and 0.45× of its time. Singleton and cached scoped resolves
allocate 0 B, asserted by
AllocationGatetests in both container modes. - Works under NativeAOT and trimming. Every PR publishes and runs an AOT sample with trim warnings as errors.
- Compile-time diagnostics instead of runtime surprises. An ambiguous constructor, a decorator over
an unregistered interface or a circular dependency is a build error, from
ZAI001toZAI021.
Install
dotnet add package ZeroAlloc.Inject.Container
The container package bundles the attributes, the generator and both generated containers. ASP.NET Core
apps that keep Microsoft DI can reference ZeroAlloc.Inject and ZeroAlloc.Inject.Generator instead.
Quick start
using Microsoft.Extensions.DependencyInjection;
using ZeroAlloc.Inject;
var services = new ServiceCollection();
services.AddMyAppServices(); // generated from the assembly name, MyApp
using var provider = services.BuildServiceProvider();
provider.GetRequiredService<IGreeter>().Greet("Ada");
public interface IClock { DateTime Now { get; } }
public interface IGreeter { void Greet(string name); }
[Singleton]
public sealed class SystemClock : IClock { public DateTime Now => DateTime.Now; }
[Transient]
public sealed class Greeter(IClock clock) : IGreeter
{
public void Greet(string name) => Console.WriteLine($"Hello {name}, it is {clock.Now:t}.");
}
For a container with no Microsoft DI at runtime, replace the first three lines with
using var provider = new ZeroAlloc.Inject.Generated.MyAppStandaloneServiceProvider();. Either way,
resolution is a generated type switch over direct new calls.
What you get
- Lifetime attributes that register a class under its interfaces, with
Asto pick the service type. - Three container modes: generated
IServiceCollectionextensions for Microsoft DI, a hybrid container that falls back to Microsoft DI for framework services, and a standalone container. See container modes. - Decorators with ordering, conditional application and optional dependencies. See decorators.
- Keyed services, open generics and property injection.
- Safe defaults:
TryAddsemantics, withAllowMultiple = trueforIHostedServiceand other multi-registrations. - Multi-assembly composition: each assembly generates its own
Add…Services()method.
Performance
BenchmarkDotNet, .NET 10, same run. Time is the ratio against Microsoft DI; allocations are exact.
| Scenario | Microsoft DI | ZA Container | ZA Standalone | Jab |
|---|---|---|---|---|
| Build the provider | 1.00× / 4,384 B | 0.67× / 2,720 B | 0.01× / 112 B | <0.01× / 40 B |
| Decorated transient | 1.00× / 48 B | 0.59× / 48 B | 0.56× / 48 B | 0.26× / 48 B |
| First scoped resolve in a new scope | 1.00× / 304 B | 0.66× / 120 B | 0.54× / 120 B | 0.54× / 120 B |
On .NET 10 Jab is the fastest in every scenario it supports; it has no property injection, no decorator attribute and no open generics. Microsoft DI is ahead of ZA with two constructor dependencies and for open generics. Every scenario, with the full numbers: docs/performance.md.
Learn more
- Documentation: inject.zeroalloc.net, including getting started, Native AOT, diagnostics and testing.
- Related packages: ZeroAlloc.Mediator, ZeroAlloc.Collections and ZeroAlloc.Templates.
- Contributing: issues and pull requests are welcome on GitHub.
- CHANGELOG.
License
MIT. See LICENSE.
| 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
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (10)
Showing the top 5 NuGet packages that depend on ZeroAlloc.Inject:
| Package | Downloads |
|---|---|
|
AI.Sentinel
Security monitoring middleware for IChatClient — prompt injection, hallucination, and operational anomaly detection with an intervention engine. |
|
|
Thalos.NET
Thalos.NET agent runtime on Microsoft Agent Framework: agent factory, tool catalog with authorization, session state machine, in-memory session store. |
|
|
BlazorNative.Core
Core contracts for BlazorNative: shell bridge and interop abstractions, the INavigationManager contract, and the DI-facing primitives shared by the renderer, HTTP bridge, and component library. |
|
|
BlazorNative.Renderer
The BlazorNative renderer: drives Blazor's RenderTree diffing into a native widget tree and emits binary UI patch frames for platform shells. NativeAOT/trim-compatible. |
|
|
ZeroAlloc.Inject.Container
A compile-time DI registration library for .NET. Roslyn source generator wires all service registrations at compile time — no reflection, no scanning. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.11.5 | 0 | 10/18/2026 |
| 1.11.4 | 831 | 10/16/2026 |
| 1.11.3 | 654 | 10/15/2026 |
| 1.11.2 | 2,259 | 10/12/2026 |
| 1.11.1 | 889 | 10/11/2026 |
| 1.11.0 | 1,443 | 10/10/2026 |
| 1.10.0 | 3,425 | 9/30/2026 |
| 1.9.1 | 151 | 9/30/2026 |
| 1.9.0 | 1,463 | 9/28/2026 |
| 1.8.0 | 1,837 | 9/25/2026 |
| 1.7.6 | 3,919 | 9/20/2026 |
| 1.7.5 | 234 | 9/20/2026 |
| 1.7.4 | 419 | 9/19/2026 |
| 1.7.3 | 5,121 | 8/12/2026 |
| 1.7.2 | 2,277 | 7/25/2026 |
| 1.7.1 | 5,432 | 5/28/2026 |
| 1.7.0 | 754 | 5/18/2026 |
| 1.6.0 | 210 | 5/18/2026 |
| 1.5.0 | 606 | 5/12/2026 |
| 1.4.2 | 327 | 5/12/2026 |