EasilyNET.AutoDependencyInjection 2.2.72

There is a newer version of this package available.
See the version list below for details.
dotnet add package EasilyNET.AutoDependencyInjection --version 2.2.72
NuGet\Install-Package EasilyNET.AutoDependencyInjection -Version 2.2.72
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="EasilyNET.AutoDependencyInjection" Version="2.2.72" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasilyNET.AutoDependencyInjection --version 2.2.72
#r "nuget: EasilyNET.AutoDependencyInjection, 2.2.72"
#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.
// Install EasilyNET.AutoDependencyInjection as a Cake Addin
#addin nuget:?package=EasilyNET.AutoDependencyInjection&version=2.2.72

// Install EasilyNET.AutoDependencyInjection as a Cake Tool
#tool nuget:?package=EasilyNET.AutoDependencyInjection&version=2.2.72
EasilyNET.AutoDependencyInjection

自动注入模块,参考 ABP 的代码实现.

如何使用
  • 使用 Nuget 包管理工具添加依赖包 EasilyNET.AutoDependencyInjection
  • a.使用特性注入服务
[DependencyInjection(ServiceLifetime.Singleton, AddSelf = true)]
public class XXXService : IXXXService
{
    // TODO: do something
    Console.WriteLine("使用特性注入服务");
    ...
}
  • b.使用接口的方式,继承 IScopedDependency, ISingletonDependency, ITransientDependency
/// <summary>
/// 测试模块
/// </summary>
public class MyTestModule : ITest, IScopedDependency
{
    /// <summary>
    /// Show
    /// </summary>
    public void Show()
    {
        Console.WriteLine("Test");
    }
}

/// <summary>
/// 测试
/// </summary>
//[IgnoreDependency]
public interface ITest
{
    /// <summary>
    /// Show函数
    /// </summary>
    void Show();
}

// 在其他地方获取服务,并执行方法
var test = context.Services.GetService<MyTestModule>();
test?.Show();
...
  • 3.继承 AppModule 类,然后显示加入到 AppWebModule 配置中
  • Step1.创建 CorsModule.cs
// 这里以跨域服务注册为例
/// <summary>
/// 配置跨域服务及中间件
/// </summary>
public class CorsModule : AppModule
{
    /// <summary>
    /// 注册和配置服务
    /// </summary>
    /// <param name="context"></param>
    public override void ConfigureServices(ConfigureServicesContext context)
    {
        var config = context.Services.GetConfiguration();
        var allow = config["AllowedHosts"] ?? "*";
        _ = context.Services.AddCors(c => c.AddPolicy("AllowedHosts", s => s.WithOrigins(allow.Split(",")).AllowAnyMethod().AllowAnyHeader()));
    }
    /// <summary>
    /// 注册中间件
    /// </summary>
    /// <param name="context"></param>
    public override void ApplicationInitialization(ApplicationContext context)
    {
        var app = context.GetApplicationBuilder();
        _ = app.UseCors("AllowedHosts");
    }
}
  • Step2.创建 AppWebModule.cs
/**
 * 要实现自动注入,一定要在这个地方添加
 */
[DependsOn(
    typeof(DependencyAppModule),
    typeof(CorsModule)
)]
public class AppWebModule : AppModule
{
    /// <summary>
    /// 注册和配置服务
    /// </summary>
    /// <param name="context"></param>
    public override void ConfigureServices(ConfigureServicesContext context)
    {
        base.ConfigureServices(context);
        _ = context.Services.AddHttpContextAccessor();
    }
    /// <summary>
    /// 注册中间件
    /// </summary>
    /// <param name="context"></param>
    public override void ApplicationInitialization(ApplicationContext context)
    {
        base.ApplicationInitialization(context);
        var app = context.GetApplicationBuilder();
        _ = app.UseAuthorization();
        // 这里可添加自己的中间件
    }
}
  • Step3.最后再 Program.cs 中添加如下内容.
// Add services to the container.
// 自动注入服务模块
builder.Services.AddApplication<AppWebModule>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) _ = app.UseDeveloperExceptionPage();

// 添加自动化注入的一些中间件.
app.InitializeApplication();

app.MapControllers();

app.Run();
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. 
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
3.24.508.112 41 5/8/2024
2.2024.428.71 76 4/28/2024
2.2024.427.1128 79 4/27/2024
2.2.72 88 4/14/2024
2.2.71 69 4/12/2024
2.2.8 69 4/26/2024
2.2.6 87 4/10/2024
2.2.5 89 3/26/2024
2.2.4 79 3/25/2024
2.2.3 86 3/24/2024
2.2.2 82 3/21/2024
2.2.1 81 3/20/2024
2.2.0 103 3/13/2024
2.1.9 112 2/21/2024
2.1.8 87 2/18/2024
2.1.7 97 2/16/2024
2.1.6 97 2/14/2024
2.1.5 86 2/14/2024
2.1.4 112 2/9/2024
2.1.3 91 2/8/2024
2.1.2 104 2/5/2024
2.1.1.2 183 12/26/2023
2.1.1.1 85 12/26/2023
2.1.1 94 12/25/2023
2.1.0 133 12/17/2023
2.0.11 161 12/6/2023
2.0.1 155 11/15/2023
2.0.0 118 11/14/2023
1.9.1 128 11/1/2023
1.9.0 106 10/19/2023
1.9.0-preview2 242 10/12/2023
1.9.0-preview1 93 10/12/2023
1.8.9 148 10/11/2023
1.8.8 143 10/11/2023
1.8.7-rc2 108 9/21/2023
1.8.7-rc1 91 9/12/2023
1.8.6 142 8/31/2023
1.8.5 205 8/25/2023
1.8.4 138 8/24/2023
1.8.3 146 8/23/2023
1.8.2 228 8/22/2023
1.8.1 164 8/18/2023
1.8.0 160 8/15/2023
1.7.9 199 8/11/2023
1.7.8 143 8/11/2023
1.7.7 163 8/10/2023
1.7.6 161 8/9/2023
1.7.5 214 8/9/2023
1.7.4 252 8/3/2023
1.7.3 155 8/1/2023
1.7.2 150 7/31/2023
1.7.1 148 7/27/2023
1.7.0 163 7/25/2023
1.6.9 143 7/25/2023
1.6.8 156 7/24/2023
1.6.7 173 7/20/2023
1.6.6 176 7/19/2023
1.6.5 148 7/19/2023
1.6.4 155 7/17/2023
1.6.3 134 7/17/2023
1.6.2 183 7/12/2023
1.6.1 191 6/30/2023
1.6.0 123 6/26/2023
1.5.9 137 6/22/2023
1.5.8 145 6/15/2023
1.5.7.1 145 6/14/2023
1.5.7 160 6/14/2023
1.5.6.2 195 6/7/2023
1.5.6.1 122 6/7/2023
1.5.6 131 6/7/2023
1.5.5.2 179 5/26/2023
1.5.5.1 144 5/26/2023
1.5.5 150 5/26/2023
1.5.4.4 156 5/25/2023
1.5.4.3 189 5/23/2023
1.5.4.2 255 5/17/2023
1.5.4.1 146 5/16/2023
1.5.4 226 5/11/2023
1.5.3 147 5/11/2023
1.5.2 102 5/10/2023
1.5.1 101 5/10/2023
1.5.0 142 5/6/2023
1.4.0 96 5/5/2023
1.3.9 108 4/23/2023
1.3.8.6 97 4/23/2023
1.3.8.5 101 4/21/2023
1.3.8.1 167 4/12/2023
1.3.8 106 4/11/2023
1.3.7 131 4/9/2023
1.3.6.3 187 4/1/2023
1.3.6.2 103 3/31/2023
1.3.6.1 104 3/31/2023
1.3.6 83 3/31/2023
1.3.5 104 3/30/2023
1.3.4.1 166 3/29/2023
1.3.4 111 3/28/2023
1.3.3 87 3/28/2023
1.3.2 120 3/26/2023
1.3.1 177 3/22/2023
1.3.0 121 3/21/2023
1.2.0 98 3/21/2023
1.1.0 115 3/17/2023
1.0.9 112 3/15/2023
1.0.8 113 3/15/2023
1.0.7 92 3/15/2023
1.0.6 112 3/13/2023
1.0.5 116 3/13/2023
1.0.4 94 3/13/2023
1.0.3 184 2/26/2023
1.0.2 103 2/26/2023
1.0.1 111 2/23/2023
1.0.0 256 2/20/2023