EasilyNET.AutoDependencyInjection 4.25.312.103

dotnet add package EasilyNET.AutoDependencyInjection --version 4.25.312.103                
NuGet\Install-Package EasilyNET.AutoDependencyInjection -Version 4.25.312.103                
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="4.25.312.103" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasilyNET.AutoDependencyInjection --version 4.25.312.103                
#r "nuget: EasilyNET.AutoDependencyInjection, 4.25.312.103"                
#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=4.25.312.103

// Install EasilyNET.AutoDependencyInjection as a Cake Tool
#tool nuget:?package=EasilyNET.AutoDependencyInjection&version=4.25.312.103                
EasilyNET.AutoDependencyInjection
  • 新增 KeyedService 支持,可在 DependencyInjectionAttribute 中看到对应的 ServiceKey 属性,用于标识服务的 Key 值.
  • 新增 WPF, WinForms, WinUI3 项目支持.(仅限于 .NET 的项目,不支持 .NET Framework)
  • 经测试是支持 WinUI 3 类型的项目的,但是需要注意的是,WinUI 3 项目的启动方式和 WPF 项目不一样,需要自行调整.
  • WPF 例子 已同步到最新代码.
  • WinForms 例子 已同步到最新代码.
  • WinUI3 例子 暂时没同步到最新版本,可以自己更新一下,目前暂时没有 WinUI 环境所以没更新.
新增特性
  • 新增 GetEnable 函数,该函数未重写的情况下默认返回 true,可通过重写该函数,实现从配置文件中读取是否启用服务.<br/>比如 SwaggerUI 服务,我们可能仅希望在预发布模式中启用,为前端工程师提供 SwaggerUI,当部署到生产环境后,通过直接修改配置文件,即可关闭 SwaggerUI.
  • 移除掉原有 Enable 属性,改为 GetEnable 函数,用于更灵活的配置服务.
中断性变更
  • 调整 ConfigureServicesApplicationInitialization 为异步方式,便于在某些时候初始化服务的时候使用异步版本.
WPF 中的使用

由于新增了 WPF 项目支持,所以在使用时需要注意以下几点: WPF 项目中,使用依赖注入,需要在 App.xaml.cs 中添加如下代码:

[STAThread]
public static void Main(string[] args)
{
    using var host = CreateHostBuilder(args).Build();
    host.InitializeApplication();
    host.Start();
    var app = new App();
    app.InitializeComponent();
    app.MainWindow = host.Services.GetRequiredService<MainWindow>();
    app.MainWindow.Visibility = Visibility.Visible;
    app.Run();
}

private static IHostBuilder CreateHostBuilder(string[] args)
{
    return Host.CreateDefaultBuilder(args)
               .ConfigureServices(sc => { sc.AddApplicationModules<AppServiceModules>(); });
}

同时还需要调整 .csproj 文件,添加如下代码:

<ItemGroup>
	<ApplicationDefinition Remove="App.xaml" />
	<Page Include="App.xaml" />
</ItemGroup>

再在 WPF 项目中,使用依赖注入,需要在 AppServiceModules.cs 中添加如下代码: 该类的使用方法和 Web 项目中的 AppWebModule.cs 一样.

[DependsOn(typeof(DependencyAppModule))]
internal sealed class AppServiceModules : AppModule { }

在 WPF 项目中,使用依赖注入,需要在 MainWindow.xaml.cs 中继承接口或者添加 DependencyInjection 特性如下代码:

// 使用特性配置注入信息
[DependencyInjection(ServiceLifetime.Singleton, AddSelf = true, SelfOnly = true)]
public partial class MainWindow : Window

注意事项
  • 需要注意的是,在 WPF 项目中,请将 AddSelf 属性设置为 true,否则会出现服务无法找到的问题,因为默认会注册实现类的父类,导致使用 host.Services.GetRequiredService<MainWindow>() 的方式无法找到服务.WinForm 项目中,没有测试,但是理论上也是一样的.
  • 由于新增 WPF 项目支持,所以调整了 IApplicationBuilder 为 IHost,因此 WEB 项目中的使用方式有细微的变化.
// 之前的使用方式
IApplicationBuilder app = context.GetApplicationBuilder();
// 现在的使用方式
IApplicationBuilder app = context.GetApplicationHost() as IApplicationBuilder;
// 或者如下方式,根据实际情况选择
WebApplication app = context.GetApplicationHost() as WebApplication;
// 在 WPF 或者 WinForm 项目中,使用如下方式
IHost app = context.GetApplicationHost();
如何使用
  • 使用 Nuget 包管理工具添加依赖包 EasilyNET.AutoDependencyInjection
  • 使用特性注入服务
[DependencyInjection(ServiceLifetime.Singleton, AddSelf = true, SelfOnly = true)]
public class XXXService : IXXXService
{
    // TODO: do something
    Console.WriteLine("使用特性注入服务");
    ...
}
  • 3.继承 AppModule 类,然后显示加入到 AppWebModule 配置中
  • Step1.创建 CorsModule.cs
// 这里以跨域服务注册为例
/// <summary>
/// 配置跨域服务及中间件
/// </summary>
public class CorsModule : AppModule
{
    // 新增函数,用于可从配置文件读取是否启用服务
    public override bool GetEnable(ConfigureServicesContext context)
    {
        var config = context.ServiceProvider.GetConfiguration();
        return config.GetSection("ServicesEnable").GetValue<bool>("Cors");
    }

    /// <summary>
    /// 注册和配置服务
    /// </summary>
    /// <param name="context"></param>
    public override async Task ConfigureServices(ConfigureServicesContext context)
    {
        var config = context.ServiceProvider.GetConfiguration();
        var allow = config["AllowedHosts"] ?? "*";
        _ = context.Services.AddCors(c => c.AddPolicy("AllowedHosts", s => s.WithOrigins(allow.Split(",")).AllowAnyMethod().AllowAnyHeader()));
        await Task.CompletedTask;
    }
    /// <summary>
    /// 注册中间件
    /// </summary>
    /// <param name="context"></param>
    public override async Task ApplicationInitialization(ApplicationContext context)
    {
        var app = context.GetApplicationBuilder() as IApplicationBuilder;
        _ = app.UseCors("AllowedHosts");
        await Task.CompletedTask;
    }
}
  • Step2.创建 AppWebModule.cs
/**
 * 要实现自动注入,一定要在这个地方添加
 */
[DependsOn(
    typeof(DependencyAppModule),
    typeof(CorsModule)
)]
public class AppWebModule : AppModule
{
    /// <summary>
    /// 注册和配置服务
    /// </summary>
    /// <param name="context"></param>
    public override async Task ConfigureServices(ConfigureServicesContext context)
    {
        _ = context.Services.AddHttpContextAccessor();
        await base.ConfigureServices(context);
    }
    /// <summary>
    /// 注册中间件
    /// </summary>
    /// <param name="context"></param>
    public override async Task ApplicationInitialization(ApplicationContext context)
    {
        var app = context.GetApplicationBuilder() as IApplicationBuilder;
        _ = app.UseAuthorization();
        // 这里可添加自己的中间件
        await base.ApplicationInitialization(context);
    }
}
  • 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.  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. 
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
4.25.312.103 126 5 days ago
4.25.227.135 95 17 days ago
4.25.221.115 85 24 days ago
4.25.212.95 106 a month ago
4.25.211.140 90 a month ago
4.25.124.223 90 2 months ago
4.25.116.110 82 2 months ago
4.25.115.121 50 2 months ago
4.25.114.172 61 2 months ago
4.25.109.111 61 2 months ago
4.25.108.182 63 2 months ago
4.25.108.160 62 2 months ago
4.25.1.1 109 2 months ago
3.24.1224.141 85 3 months ago
3.24.1216.116 112 3 months ago
3.24.1206.100 98 3 months ago
3.24.1205.171 105 3 months ago
3.24.1202.150 97 3 months ago
3.24.1126.231 100 4 months ago
3.24.1126.172 96 4 months ago
3.24.1126.114 94 4 months ago
3.24.1126.104 87 4 months ago
3.24.1125.181 82 4 months ago
3.24.1125.104 97 4 months ago
3.24.1121.183 92 4 months ago
3.24.1120.183 96 4 months ago
3.24.1119.31 92 4 months ago
3.24.1115.143 84 4 months ago
3.24.1113.100 105 4 months ago
3.24.1112.125 89 4 months ago
3.24.1107.140 102 4 months ago
3.24.1107.54 90 4 months ago
3.24.1107.34 90 4 months ago
3.24.1105.111 93 4 months ago
3.24.1103.31 99 4 months ago
3.24.1103 95 4 months ago
3.24.1031.135 98 5 months ago
3.24.1031.112 94 5 months ago
3.24.1031.104 97 5 months ago
3.24.1029.142 102 5 months ago
3.24.1025.30 98 5 months ago
3.24.1022.142 83 5 months ago
3.24.1018.204 151 5 months ago
3.24.1018.175 143 5 months ago
3.24.1018.166 142 5 months ago
3.24.1018.93 155 5 months ago
3.24.1017.42 115 5 months ago
3.24.1016.161 115 5 months ago
3.24.1015.231 107 5 months ago
3.24.1015.14 92 5 months ago
3.24.1012.114 94 5 months ago
3.24.1009.115 100 5 months ago
3.24.1008.160 97 5 months ago
3.24.1008.133 97 5 months ago
3.24.1007.185 101 5 months ago
3.24.1003.33 102 5 months ago
3.24.1002.162 99 5 months ago
3.24.929.143 97 6 months ago
3.24.929.141 84 6 months ago
3.24.929.131 97 6 months ago
3.24.929.122 101 6 months ago
3.24.926.184 100 6 months ago
3.24.926.182 90 6 months ago
3.24.926.175 96 6 months ago
3.24.924.160 102 6 months ago
3.24.924.133 110 6 months ago
3.24.924.124 95 6 months ago
3.24.924.10 102 6 months ago
3.24.924.1 91 6 months ago
3.24.923.234 94 6 months ago
3.24.923.232 100 6 months ago
3.24.923.155 104 6 months ago
3.24.919.92 115 6 months ago
3.24.914.125 124 6 months ago
3.24.914.115 104 6 months ago
3.24.914.111 108 6 months ago
3.24.911.95 113 6 months ago
3.24.908.215 98 6 months ago
3.24.904.200 108 6 months ago
3.24.828.163 132 7 months ago
3.24.820.173 115 7 months ago
3.24.814.92 136 7 months ago
3.24.812.115 114 7 months ago
3.24.802.100 72 7 months ago
3.24.801.162 86 8 months ago
3.24.801.160 79 8 months ago
3.24.801.155 81 8 months ago
3.24.801.153 126 8 months ago
3.24.730.164 85 8 months ago
3.24.730.91 80 8 months ago
3.24.724.91 81 8 months ago
3.24.718.105 97 8 months ago
3.24.716.95 132 8 months ago
3.24.712.94 116 8 months ago
3.24.710.14 114 8 months ago
3.24.709.105 122 8 months ago
3.24.704.94 125 8 months ago
3.24.701.90 111 9 months ago
3.24.628.114 116 9 months ago
3.24.627.145 116 9 months ago
3.24.620.160 110 9 months ago
3.24.613.115 104 9 months ago
3.24.612.95 106 9 months ago
3.24.528.90 110 10 months ago
3.24.522.84 123 10 months ago
3.24.512.213 110 5/12/2024
3.24.508.112 136 5/8/2024
2.2024.428.71 129 4/28/2024
2.2024.427.1128 120 4/27/2024
2.2.72 126 4/14/2024
2.2.71 105 4/12/2024
2.2.8 109 4/26/2024
2.2.6 126 4/10/2024
2.2.5 132 3/26/2024
2.2.4 118 3/25/2024
2.2.3 130 3/24/2024
2.2.2 125 3/21/2024
2.2.1 126 3/20/2024
2.2.0 148 3/13/2024
2.1.9 157 2/21/2024
2.1.8 132 2/18/2024
2.1.7 146 2/16/2024
2.1.6 163 2/14/2024
2.1.5 127 2/14/2024
2.1.4 156 2/9/2024
2.1.3 130 2/8/2024
2.1.2 181 2/5/2024
2.1.1.2 229 12/26/2023
2.1.1.1 126 12/26/2023
2.1.1 139 12/25/2023
2.1.0 153 12/17/2023
2.0.11 207 12/6/2023
2.0.1 208 11/15/2023
2.0.0 160 11/14/2023
1.9.1 172 11/1/2023
1.9.0 144 10/19/2023
1.9.0-preview2 354 10/12/2023
1.9.0-preview1 112 10/12/2023
1.8.9 190 10/11/2023
1.8.8 183 10/11/2023
1.8.7-rc2 144 9/21/2023
1.8.7-rc1 133 9/12/2023
1.8.6 183 8/31/2023
1.8.5 876 8/25/2023
1.8.4 173 8/24/2023
1.8.3 196 8/23/2023
1.8.2 267 8/22/2023
1.8.1 207 8/18/2023
1.8.0 198 8/15/2023
1.7.9 231 8/11/2023
1.7.8 175 8/11/2023
1.7.7 187 8/10/2023
1.7.6 202 8/9/2023
1.7.5 253 8/9/2023
1.7.4 289 8/3/2023
1.7.3 194 8/1/2023
1.7.2 189 7/31/2023
1.7.1 188 7/27/2023
1.7.0 204 7/25/2023
1.6.9 183 7/25/2023
1.6.8 195 7/24/2023
1.6.7 216 7/20/2023
1.6.6 214 7/19/2023
1.6.5 188 7/19/2023
1.6.4 191 7/17/2023
1.6.3 164 7/17/2023
1.6.2 219 7/12/2023
1.6.1 229 6/30/2023
1.6.0 162 6/26/2023
1.5.9 177 6/22/2023
1.5.8 184 6/15/2023
1.5.7.1 184 6/14/2023
1.5.7 181 6/14/2023
1.5.6.2 237 6/7/2023
1.5.6.1 164 6/7/2023
1.5.6 164 6/7/2023
1.5.5.2 224 5/26/2023
1.5.5.1 182 5/26/2023
1.5.5 182 5/26/2023
1.5.4.4 195 5/25/2023
1.5.4.3 236 5/23/2023
1.5.4.2 301 5/17/2023
1.5.4.1 192 5/16/2023
1.5.4 268 5/11/2023
1.5.3 189 5/11/2023
1.5.2 132 5/10/2023
1.5.1 130 5/10/2023
1.5.0 172 5/6/2023
1.4.0 127 5/5/2023
1.3.9 140 4/23/2023
1.3.8.6 122 4/23/2023
1.3.8.5 132 4/21/2023
1.3.8.1 198 4/12/2023
1.3.8 137 4/11/2023
1.3.7 163 4/9/2023
1.3.6.3 219 4/1/2023
1.3.6.2 135 3/31/2023
1.3.6.1 136 3/31/2023
1.3.6 109 3/31/2023
1.3.5 133 3/30/2023
1.3.4.1 195 3/29/2023
1.3.4 142 3/28/2023
1.3.3 118 3/28/2023
1.3.2 151 3/26/2023
1.3.1 205 3/22/2023
1.3.0 144 3/21/2023
1.2.0 130 3/21/2023
1.1.0 144 3/17/2023
1.0.9 140 3/15/2023
1.0.8 142 3/15/2023
1.0.7 121 3/15/2023
1.0.6 140 3/13/2023
1.0.5 145 3/13/2023
1.0.4 121 3/13/2023
1.0.3 210 2/26/2023
1.0.2 132 2/26/2023
1.0.1 143 2/23/2023
1.0.0 302 2/20/2023