Smart.Inject 5.2.0

dotnet add package Smart.Inject --version 5.2.0
                    
NuGet\Install-Package Smart.Inject -Version 5.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="Smart.Inject" Version="5.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Smart.Inject" Version="5.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Smart.Inject" />
                    
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 Smart.Inject --version 5.2.0
                    
#r "nuget: Smart.Inject, 5.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 Smart.Inject@5.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=Smart.Inject&version=5.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Smart.Inject&version=5.2.0
                    
Install as a Cake Tool

Smart.Inject

NuGet

English | 中文

<a name="english"></a>

English

Smart.Inject is a smart dependency injection framework for .NET. It simplifies service registration by scanning assemblies and automatically registering services decorated with specific attributes. It supports flexible registration schemes, lifecycle management, and custom configuration logic, making your Program.cs cleaner and more maintainable.

Features

  • Auto-Registration: Automatically scans and registers services marked with attributes like [ServiceOfScoped], [ServiceOfTransient], or [ServiceOfSingleton].
  • Flexible Schemes: Control how services are registered (as interfaces, as self, as base classes, etc.) using InjectScheme.
  • Custom Configuration: Implement IServiceConfigure to add complex registration logic that runs during the scanning process.
  • Hosted Services: Automatically detects and registers IHostedService implementations.
  • Exclusion: Use [IgnoreInject] to exclude specific classes from auto-registration.
  • Lifecycle Management: Full support for Transient, Scoped, and Singleton lifecycles.

Installation

Install the package via NuGet:

dotnet add package Smart.Inject

Usage Examples

1. Enable Scanning

In your Program.cs (or Startup.cs), call ScanRegisterServices to enable the auto-registration features.

using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Enable Smart.Inject scanning
builder.Services.ScanRegisterServices(builder.Configuration);

var app = builder.Build();
2. Attribute-Based Registration

Decorate your services with lifecycle attributes. You don't need to manually register them in Program.cs.

using Smart.Inject;

// Registers as Scoped (default scheme: registers as IUserRepository if exists, else self)
[ServiceOfScoped]
public class UserRepository : IUserRepository 
{
    // ...
}

// Registers as Singleton
[ServiceOfSingleton]
public class CacheService : ICacheService
{
    // ...
}

// Registers as Transient
[ServiceOfTransient]
public class RandomGenerator
{
    // ...
}
3. Injection Schemes

Use InjectScheme to control how the service is registered in the DI container.

using Smart.Inject;

// InjectScheme.OnlySelf: Registers only as 'SimpleService', ignoring interfaces
[ServiceOfScoped(InjectScheme.OnlySelf)]
public class SimpleService : ISimpleService
{
}

// InjectScheme.All: Registers as 'ComplexService', 'IComplexService', and 'BaseService'
[ServiceOfScoped(InjectScheme.All)]
public class ComplexService : BaseService, IComplexService
{
}
4. Custom Configuration

If you need logic beyond simple attribute registration, implement IServiceConfigure. The framework will find this class and execute the Configure method.

using Smart.Inject;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;

public class MyServiceConfiguration : IServiceConfigure
{
    public void Configure(IServiceCollection services, IConfiguration configuration)
    {
        // Custom registration logic
        services.AddHttpClient("MyClient", client => 
        {
            client.BaseAddress = new Uri("https://api.example.com");
        });
        
        // Read config and register
        var settings = configuration.GetSection("MySettings").Get<MySettings>();
        services.AddSingleton(settings);
    }
}
5. Ignore Registration

Prevent a service from being scanned and registered.

using Smart.Inject;

[IgnoreInject]
public class InternalHelper
{
    // This will NOT be registered automatically
}

<a name="chinese"></a>

中文

Smart.Inject 是一个适用于 .NET 的智能依赖注入框架。它通过扫描程序集并自动注册带有特定特性的服务,简化了服务注册过程。它支持灵活的注册方案、生命周期管理和自定义配置逻辑,让你的 Program.cs 更加整洁和易于维护。

功能特性

  • 自动注册:自动扫描并注册标记了 [ServiceOfScoped][ServiceOfTransient][ServiceOfSingleton] 等特性的服务。
  • 灵活方案:使用 InjectScheme 控制服务的注册方式(注册为接口、自身、基类等)。
  • 自定义配置:实现 IServiceConfigure 接口,在扫描过程中执行复杂的注册逻辑。
  • 后台服务:自动检测并注册 IHostedService 的实现。
  • 排除注册:使用 [IgnoreInject] 特性排除不需要自动注册的类。
  • 生命周期管理:全面支持 Transient、Scoped 和 Singleton 生命周期。

安装

通过 NuGet 安装:

dotnet add package Smart.Inject

使用示例

1. 启用扫描

Program.cs(或 Startup.cs)中,调用 ScanRegisterServices 以启用自动注册功能。

using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// 启用 Smart.Inject 扫描
builder.Services.ScanRegisterServices(builder.Configuration);

var app = builder.Build();
2. 基于特性的注册

使用生命周期特性标记你的服务。你不再需要在 Program.cs 中手动注册它们。

using Smart.Inject;

// 注册为 Scoped (默认方案:如果存在接口则注册为 IUserRepository,否则注册为自身)
[ServiceOfScoped]
public class UserRepository : IUserRepository 
{
    // ...
}

// 注册为 Singleton
[ServiceOfSingleton]
public class CacheService : ICacheService
{
    // ...
}

// 注册为 Transient
[ServiceOfTransient]
public class RandomGenerator
{
    // ...
}
3. 注册方案 (Injection Schemes)

使用 InjectScheme 控制服务在 DI 容器中的注册方式。

using Smart.Inject;

// InjectScheme.OnlySelf: 仅注册为 'SimpleService',忽略接口
[ServiceOfScoped(InjectScheme.OnlySelf)]
public class SimpleService : ISimpleService
{
}

// InjectScheme.All: 同时注册为 'ComplexService'、'IComplexService' 和 'BaseService'
[ServiceOfScoped(InjectScheme.All)]
public class ComplexService : BaseService, IComplexService
{
}
4. 自定义配置

如果你需要超出简单特性注册的逻辑,请实现 IServiceConfigure 接口。框架会自动发现该类并执行 Configure 方法。

using Smart.Inject;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;

public class MyServiceConfiguration : IServiceConfigure
{
    public void Configure(IServiceCollection services, IConfiguration configuration)
    {
        // 自定义注册逻辑
        services.AddHttpClient("MyClient", client => 
        {
            client.BaseAddress = new Uri("https://api.example.com");
        });
        
        // 读取配置并注册
        var settings = configuration.GetSection("MySettings").Get<MySettings>();
        services.AddSingleton(settings);
    }
}
5. 忽略注册

防止服务被扫描和注册。

using Smart.Inject;

[IgnoreInject]
public class InternalHelper
{
    // 这个类将不会被自动注册
}

Developed by zenglei

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
5.2.0 84 3/22/2026
5.2.0-beta.3 58 2/8/2026
5.2.0-beta.2 58 12/30/2025
5.2.0-beta.1 241 11/10/2025
5.1.1 206 10/15/2025
5.1.0 226 7/13/2025
5.0.0 185 4/5/2025
4.0.0 204 3/27/2025
3.1.0 255 3/20/2025
3.0.3 218 3/16/2025
3.0.2 171 2/26/2025
3.0.1 201 2/15/2025
3.0.0 185 2/15/2025
2.0.3 182 2/13/2025
2.0.2 185 2/9/2025
2.0.1 211 12/7/2024
2.0.0 192 11/26/2024
1.0.0.5 182 10/9/2024
1.0.0.4 184 9/25/2024
1.0.0.3 213 9/24/2024
Loading failed