Ling.AutoInject 1.2.0

Additional Details

Source generator compatibility issue: these versions require Roslyn 4.14 and fail with CS9057 on supported .NET 6/8 and older Visual Studio compiler hosts. Upgrade to Ling.AutoInject 1.4.1, which supports Roslyn 4.3+.

There is a newer version of this package available.
See the version list below for details.
dotnet add package Ling.AutoInject --version 1.2.0
                    
NuGet\Install-Package Ling.AutoInject -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ling.AutoInject" Version="1.2.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ling.AutoInject" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Ling.AutoInject">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Ling.AutoInject --version 1.2.0
                    
#r "nuget: Ling.AutoInject, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Ling.AutoInject@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Ling.AutoInject&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Ling.AutoInject&version=1.2.0
                    
Install as a Cake Tool

Ling.AutoInject

English | 简体中文

Ling.AutoInject provides attribute-driven registration helpers and integrates with a source generator to emit IServiceCollection extension methods that register services discovered via attributes at compile time.

Features

  • Attributes: SingletonService, ScopedService, TransientService for simple, declarative registration.
  • Optional service typing and keyed registration support.
  • Configurable generated method name, host class and namespace through an assembly-level AutoInjectConfig attribute.
  • Service replacement: use Replace = true to replace existing registrations instead of skipping when a service is already registered.
  • Class-level customization via AutoInjectExtensionsAttribute for control over method generation behavior, including optional IConfiguration parameter support.
  • Complementary analyzers to surface common mistakes and invalid configurations in the IDE.

Installation

via .NET CLI:

dotnet add package Ling.AutoInject

via Package Manager Console:

Install-Package Ling.AutoInject

Usage

  1. Decorate implementation types:

    using Ling.AutoInject;
    
    [SingletonService]
    public class MyService { }
    
    [ScopedService(typeof(IFoo))]
    public class MyService : IFoo { }
    
    [TransientService(ServiceKey = "k1")]
    public class MyService { }
    
  2. (Optional) Configure generator output naming:

    [assembly: Ling.AutoInject.AutoInjectConfig(
        MethodName = "AddCustomServices",
        ClassName = "ServiceExtensions",
        Namespace = "MyNamespace")]
    
  3. Call the generated extension in Program / Startup:

    services.Add[MyAssembly]Services();
    // or
    services.AddCustomServices();
    

Notes

  • If no service type is specified, the implementation type is registered as itself.
  • If a service type is specified, the generator registers the mapping from service interface to implementation.
  • Keyed registration requires the DI Abstractions package to support keyed APIs; analyzers warn when unsupported.
  • A runnable record-type registration example is available in the repository under samples/Ling.AutoInject.Sample.

Advanced features

Replace existing registrations

Use the Replace property to replace existing service registrations instead of using TryAdd methods:

[SingletonService(typeof(IFoo), Replace = true)]
public class MyService : IFoo { }

This generates services.Replace(ServiceDescriptor.Singleton<IFoo, MyService>()) instead of services.TryAddSingleton<IFoo, MyService>().

Using AutoInjectExtensionsAttribute

Instead of using the assembly-level AutoInjectConfig, you can decorate a static partial class with AutoInjectExtensionsAttribute for more control:

using Ling.AutoInject;

namespace MyNamespace
{
    [AutoInjectExtensions(MethodName = "AddCustomServices")]
    public static partial class MyServiceExtensions { }
}

This generates a partial class with the specified method name in the same namespace as the decorated class.

Including IConfiguration

Use IncludeConfiguration = true to generate a method that accepts an IConfiguration parameter:

[AutoInjectExtensions(MethodName = "AddCustomServices", IncludeConfiguration = true)]
public static partial class MyServiceExtensions { }

This generates:

public static IServiceCollection AddCustomServices(this IServiceCollection services, IConfiguration configuration)
{
    // ...
    AddAdditionalServices(services, configuration);
    return services;
}

static partial void AddAdditionalServices(IServiceCollection services, IConfiguration configuration);

You can implement the AddAdditionalServices partial method to add custom service registrations that require configuration.

Roadmap

The project will continue to evolve around three priorities: generator correctness, expressive registration capabilities, and integration with the broader .NET dependency injection ecosystem.

Version 1.2 — Correctness and compatibility

  • Unify source generator and analyzer behavior for framework and package version detection.
  • Improve diagnostics for unsupported registration targets, including abstract, static, and generic types.
  • Extend discovery to support record class types and extension hosts declared in the global namespace.
  • Strengthen validation for duplicate registrations, conflicting lifetimes, service type mismatches, and configuration conflicts.
  • Add runtime integration tests in addition to generated-source snapshot tests.

Version 1.3 — Registration capabilities

  • Introduce an optional unified AutoInject attribute while preserving the existing lifetime-specific attributes.
  • Support registration of all implemented interfaces through declarative configuration.
  • Add explicit registration strategies, including Add, TryAdd, Replace, and TryAddEnumerable.
  • Improve keyed service support with stronger key validation and more consistent registration semantics.

Version 1.4 — Configuration and modularization

  • Add attribute-driven Options registration and configuration binding for IOptions<T>-style options classes.
  • Support configuration validation, including startup validation and data annotation validation where applicable.
  • Support module-level registration methods for large applications and multi-module solutions.
  • Allow multiple generated registration entry points where project organization requires them.

Version 2.0 — Advanced dependency injection scenarios

  • Support open generic registrations with appropriate compile-time validation.
  • Support factory, delegate, and instance registrations.
  • Provide decorator and interception-oriented registration capabilities.
  • Add conditional registrations based on configuration or environment.
  • Explore dependency graph diagnostics and visualization for generated registrations.

The roadmap is subject to change based on compatibility requirements, community feedback, and the evolution of the Microsoft.Extensions.DependencyInjection APIs. Backward compatibility with existing attributes and generated registration entry points will remain a primary consideration.

Contributing

  • PRs and issues welcome. Include tests for analyzer/generator changes.

License

  • MIT License.
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Ling.AutoInject:

Package Downloads
Ling.AutoInject.Options

Options registration contracts for Ling.AutoInject.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.1 134 9/11/2026
1.4.0 209 9/11/2026 1.4.0 is deprecated.
1.3.0 115 9/5/2026 1.3.0 is deprecated.
1.2.0 112 9/4/2026 1.2.0 is deprecated.
1.1.0 244 3/26/2026 1.1.0 is deprecated.
1.0.0 331 11/14/2025 1.0.0 is deprecated.