ZeroAlloc.Inject.Container
1.11.5
dotnet add package ZeroAlloc.Inject.Container --version 1.11.5
NuGet\Install-Package ZeroAlloc.Inject.Container -Version 1.11.5
<PackageReference Include="ZeroAlloc.Inject.Container" Version="1.11.5" />
<PackageVersion Include="ZeroAlloc.Inject.Container" Version="1.11.5" />
<PackageReference Include="ZeroAlloc.Inject.Container" />
paket add ZeroAlloc.Inject.Container --version 1.11.5
#r "nuget: ZeroAlloc.Inject.Container, 1.11.5"
#:package ZeroAlloc.Inject.Container@1.11.5
#addin nuget:?package=ZeroAlloc.Inject.Container&version=1.11.5
#tool nuget:?package=ZeroAlloc.Inject.Container&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
- Microsoft.Extensions.DependencyInjection (>= 10.0.13)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.13)
- ZeroAlloc.Collections (>= 1.1.5)
- ZeroAlloc.Inject (>= 1.11.5)
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.13)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.13)
- ZeroAlloc.Collections (>= 1.1.5)
- ZeroAlloc.Inject (>= 1.11.5)
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.13)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.13)
- ZeroAlloc.Collections (>= 1.1.5)
- ZeroAlloc.Inject (>= 1.11.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.11.5 | 37 | 10/18/2026 |
| 1.11.4 | 95 | 10/16/2026 |
| 1.11.3 | 57 | 10/15/2026 |
| 1.11.2 | 101 | 10/12/2026 |
| 1.11.1 | 90 | 10/11/2026 |
| 1.11.0 | 89 | 10/10/2026 |
| 1.10.0 | 113 | 9/30/2026 |
| 1.9.1 | 135 | 9/30/2026 |
| 1.9.0 | 110 | 9/28/2026 |
| 1.8.0 | 141 | 9/25/2026 |
| 1.7.6 | 109 | 9/20/2026 |
| 1.7.5 | 93 | 9/20/2026 |
| 1.7.4 | 94 | 9/19/2026 |
| 1.7.3 | 125 | 8/12/2026 |
| 1.7.2 | 127 | 7/25/2026 |
| 1.7.1 | 130 | 5/28/2026 |
| 1.7.0 | 127 | 5/18/2026 |
| 1.6.0 | 124 | 5/18/2026 |
| 1.5.0 | 126 | 5/12/2026 |
| 1.4.2 | 125 | 5/12/2026 |