Skip to content
SwevoPublic

About

Compile-time DI auto-registration for .NET — [Scoped]/[Singleton]/[Transient] generates IServiceCollection registration code at build time. Zero reflection, AOT-safe.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Repository files navigation

AutoWire

NuGet NuGet Downloads CI Native AOT: verified in CI License: MIT

Put [Scoped] on a class. AutoWire writes the services.AddScoped<…>() line for you at build time, and tells you before you run when your DI graph is wrong.

  • No reflection, no assembly scanning. Plain generated code, so it works with Native AOT and trimming (checked on every build).
  • DI mistakes become build errors and IDE warnings — captive dependencies, circular dependencies, missing registrations, wrong service types — most with a one-click fix.
  • Migrate in one command from Scrutor, Autofac or hand-written AddScoped lines.
  • Runs on any .NET 8 SDK or later (Visual Studio 2022 17.8+).

Install

dotnet add package AutoWire

Before and after

// Before: Program.cs grows forever, and breaks when someone forgets a line
builder.Services.AddScoped<IOrderService, OrderService>();
builder.Services.AddSingleton<ICache, RedisCache>();
builder.Services.AddTransient<IReportGenerator, PdfReportGenerator>();
// ... 40 more lines
// After: the registration lives next to the class
[Scoped]    public class OrderService : IOrderService { }
[Singleton] public class RedisCache : ICache { }
[Transient] public class PdfReportGenerator : IReportGenerator { }

// Program.cs: one line, forever
builder.Services.AddAutoWireServices();   // using AutoWire;

The generated code is ordinary IServiceCollection calls in obj/ — exactly what you would have written by hand.

Mistakes it catches while you type

[Singleton]
public class ReportingService(IOrderService orders) { }
//        ⚠ AW004: Singleton 'ReportingService' depends on Scoped service 'IOrderService'
//          (captive dependency). Quick fix: inject IServiceScopeFactory.
ID What it catches
AW003 ❌ [Scoped(typeof(IFoo))] on a class that doesn't implement IFoo
AW004 / AW009 ⚠ Singleton or hosted service capturing a scoped service
AW013 ⚠ Constructor needs a service nobody registered
AW016 ❌ Circular dependency between services
AW006 / AW008 ⚠ Disposable transients; singletons holding DbContext or IHttpContextAccessor

All 26 diagnostics and their fixes →

How it compares

Scrutor Injectio AutoWire
Registration happens at startup (reflection) at build time at build time
Native AOT / trimming ✗ not stated ✓ verified in CI
Decorators (incl. open generic) ✓ ✓ ✓
Build-time checks for captive, circular and missing dependencies ✗ not stated ✓ with code fixes
Automated migration from Scrutor / Autofac — — ✓
Minimum toolchain any Roslyn 4.14 (VS 17.14 / .NET 9.0.300 SDK) .NET 8 SDK / VS 17.8

Based on each project's own documentation, November 2026. Corrections welcome via an issue.

Migrating an existing app

dotnet tool install --global Swevo.AutoWire.Migrator
autowire-migrate --path .            # report only
autowire-migrate --path . --apply    # rewrite safe cases to attributes

It understands Scrutor Scan(...)/Decorate(...), Autofac RegisterType/RegisterAssemblyTypes/RegisterDecorator, and plain AddScoped/AddSingleton/AddTransient, and rates each finding AutoConverted, NeedsReview or Manual. In the IDE, AW018 and AW026 offer the same migration as quick fixes. Migration guide →

Everything else it does

  • Lifetimes: [Scoped], [Singleton], [Transient], and Try* variants for library authors — reference
  • Keyed services with string or enum keys — reference
  • Decorators with ordering, including open-generic decorators — reference
  • Open generics — Repository<T> : IRepository<T> — reference
  • Convention scanning by namespace, and other assemblies — reference
  • Profiles, conditions and modules for per-environment and opt-in registration — reference
  • Factories, hosted services, options binding, typed HttpClients, FluentValidation validators — reference
  • Compile-time interceptors (AOP without Castle) and minimal API endpoints — reference
  • Registration manifest and Mermaid dependency graph for CI diffs and docs — reference
  • Test helpers — services.OverrideService<IFoo, FakeFoo>() — reference

Requirements

  • Build: .NET 8 SDK or later, Visual Studio 2022 17.8+ or Rider 2023.3+.
  • App: anything using Microsoft.Extensions.DependencyInjection — ASP.NET Core, Worker Services, MAUI, Blazor, console. Targets net6.0+ and netstandard2.0+. Keyed services need DI 8.0+.
  • AutoWire adds nothing to your output: it ships only a source generator and analyzers.

Learn more

Author

Made by Justin Bannister, who also writes other compile-time .NET libraries and consults on Polly v8 resilience, Azure architecture and clean .NET design: solidqualitysolutions.com · LinkedIn.

License

MIT © Justin Bannister

About

Compile-time DI auto-registration for .NET — [Scoped]/[Singleton]/[Transient] generates IServiceCollection registration code at build time. Zero reflection, AOT-safe.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages