EasilyNET.AutoDependencyInjection
3.24.1031.104
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package EasilyNET.AutoDependencyInjection --version 3.24.1031.104
NuGet\Install-Package EasilyNET.AutoDependencyInjection -Version 3.24.1031.104
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="3.24.1031.104" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasilyNET.AutoDependencyInjection --version 3.24.1031.104
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EasilyNET.AutoDependencyInjection, 3.24.1031.104"
#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=3.24.1031.104 // Install EasilyNET.AutoDependencyInjection as a Cake Tool #tool nuget:?package=EasilyNET.AutoDependencyInjection&version=3.24.1031.104
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
EasilyNET.AutoDependencyInjection
- 新增KeyedService支持,可在 DependencyInjectionAttribute 中看到对应的 ServiceKey 属性,用于标识服务的 Key 值.
- 新增 WPF 项目支持,理论上也支持 WinForm 项目,但是没有测试,使用时请注意.(仅限于 .NET 的项目,不支持 .NET Framework)
- 经测试是支持 WinUI 3 类型的项目的,但是需要注意的是,WinUI 3 项目的启动方式和 WPF 项目不一样,需要自行调整.
中断性变更
- 移除使用 IScopedDependency, ISingletonDependency, ITransientDependency 接口的方式,仅使用特性注入的方式.保持设计简洁性.
变化
由于新增了 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
{
/// <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() as IApplicationBuilder;
_ = 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() as IApplicationBuilder;
_ = 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 | Versions 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.
-
net8.0
- EasilyNET.AutoDependencyInjection.Core (>= 3.24.1031.104)
- EasilyNET.Core (>= 3.24.1031.104)
-
net9.0
- EasilyNET.AutoDependencyInjection.Core (>= 3.24.1031.104)
- EasilyNET.Core (>= 3.24.1031.104)
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.1224.141 | 49 | 12/24/2024 |
3.24.1216.116 | 93 | 12/16/2024 |
3.24.1206.100 | 87 | 12/6/2024 |
3.24.1205.171 | 93 | 12/5/2024 |
3.24.1202.150 | 83 | 12/2/2024 |
3.24.1126.231 | 92 | 11/26/2024 |
3.24.1126.172 | 86 | 11/26/2024 |
3.24.1126.114 | 81 | 11/26/2024 |
3.24.1126.104 | 77 | 11/26/2024 |
3.24.1125.181 | 72 | 11/25/2024 |
3.24.1125.104 | 87 | 11/25/2024 |
3.24.1121.183 | 82 | 11/21/2024 |
3.24.1120.183 | 86 | 11/20/2024 |
3.24.1119.31 | 81 | 11/18/2024 |
3.24.1115.143 | 74 | 11/15/2024 |
3.24.1113.100 | 96 | 11/13/2024 |
3.24.1112.125 | 78 | 11/12/2024 |
3.24.1107.140 | 93 | 11/7/2024 |
3.24.1107.54 | 79 | 11/7/2024 |
3.24.1107.34 | 80 | 11/7/2024 |
3.24.1105.111 | 85 | 11/5/2024 |
3.24.1103.31 | 90 | 11/2/2024 |
3.24.1103 | 85 | 11/2/2024 |
3.24.1031.135 | 86 | 10/31/2024 |
3.24.1031.112 | 82 | 10/31/2024 |
3.24.1031.104 | 85 | 10/31/2024 |
3.24.1029.142 | 90 | 10/29/2024 |
3.24.1025.30 | 88 | 10/24/2024 |
3.24.1022.142 | 73 | 10/22/2024 |
3.24.1018.204 | 144 | 10/18/2024 |
3.24.1018.175 | 136 | 10/18/2024 |
3.24.1018.166 | 137 | 10/18/2024 |
3.24.1018.93 | 148 | 10/18/2024 |
3.24.1017.42 | 108 | 10/16/2024 |
3.24.1016.161 | 106 | 10/16/2024 |
3.24.1015.231 | 95 | 10/15/2024 |
3.24.1015.14 | 87 | 10/14/2024 |
3.24.1012.114 | 87 | 10/12/2024 |
3.24.1009.115 | 93 | 10/9/2024 |
3.24.1008.160 | 90 | 10/8/2024 |
3.24.1008.133 | 93 | 10/8/2024 |
3.24.1007.185 | 94 | 10/7/2024 |
3.24.1003.33 | 96 | 10/2/2024 |
3.24.1002.162 | 95 | 10/2/2024 |
3.24.929.143 | 91 | 9/29/2024 |
3.24.929.141 | 79 | 9/29/2024 |
3.24.929.131 | 92 | 9/29/2024 |
3.24.929.122 | 97 | 9/29/2024 |
3.24.926.184 | 94 | 9/26/2024 |
3.24.926.182 | 84 | 9/26/2024 |
3.24.926.175 | 90 | 9/26/2024 |
3.24.924.160 | 95 | 9/24/2024 |
3.24.924.133 | 104 | 9/24/2024 |
3.24.924.124 | 89 | 9/24/2024 |
3.24.924.10 | 94 | 9/23/2024 |
3.24.924.1 | 83 | 9/23/2024 |
3.24.923.234 | 86 | 9/23/2024 |
3.24.923.232 | 94 | 9/23/2024 |
3.24.923.155 | 93 | 9/23/2024 |
3.24.919.92 | 105 | 9/19/2024 |
3.24.914.125 | 120 | 9/14/2024 |
3.24.914.115 | 102 | 9/14/2024 |
3.24.914.111 | 103 | 9/14/2024 |
3.24.911.95 | 109 | 9/11/2024 |
3.24.908.215 | 93 | 9/8/2024 |
3.24.904.200 | 104 | 9/4/2024 |
3.24.828.163 | 128 | 8/28/2024 |
3.24.820.173 | 113 | 8/20/2024 |
3.24.814.92 | 131 | 8/14/2024 |
3.24.812.115 | 110 | 8/12/2024 |
3.24.802.100 | 69 | 8/2/2024 |
3.24.801.162 | 82 | 8/1/2024 |
3.24.801.160 | 75 | 8/1/2024 |
3.24.801.155 | 77 | 8/1/2024 |
3.24.801.153 | 121 | 8/1/2024 |
3.24.730.164 | 78 | 7/30/2024 |
3.24.730.91 | 76 | 7/30/2024 |
3.24.724.91 | 79 | 7/24/2024 |
3.24.718.105 | 92 | 7/18/2024 |
3.24.716.95 | 122 | 7/16/2024 |
3.24.712.94 | 110 | 7/12/2024 |
3.24.710.14 | 110 | 7/9/2024 |
3.24.709.105 | 117 | 7/9/2024 |
3.24.704.94 | 119 | 7/4/2024 |
3.24.701.90 | 105 | 7/1/2024 |
3.24.628.114 | 111 | 6/28/2024 |
3.24.627.145 | 111 | 6/27/2024 |
3.24.620.160 | 105 | 6/20/2024 |
3.24.613.115 | 101 | 6/13/2024 |
3.24.612.95 | 101 | 6/12/2024 |
3.24.528.90 | 106 | 5/28/2024 |
3.24.522.84 | 120 | 5/22/2024 |
3.24.512.213 | 107 | 5/12/2024 |
3.24.508.112 | 132 | 5/8/2024 |
2.2024.428.71 | 126 | 4/28/2024 |
2.2024.427.1128 | 117 | 4/27/2024 |
2.2.72 | 123 | 4/14/2024 |
2.2.71 | 102 | 4/12/2024 |
2.2.8 | 106 | 4/26/2024 |
2.2.6 | 123 | 4/10/2024 |
2.2.5 | 129 | 3/26/2024 |
2.2.4 | 115 | 3/25/2024 |
2.2.3 | 127 | 3/24/2024 |
2.2.2 | 122 | 3/21/2024 |
2.2.1 | 123 | 3/20/2024 |
2.2.0 | 144 | 3/13/2024 |
2.1.9 | 150 | 2/21/2024 |
2.1.8 | 124 | 2/18/2024 |
2.1.7 | 139 | 2/16/2024 |
2.1.6 | 156 | 2/14/2024 |
2.1.5 | 120 | 2/14/2024 |
2.1.4 | 153 | 2/9/2024 |
2.1.3 | 124 | 2/8/2024 |
2.1.2 | 178 | 2/5/2024 |
2.1.1.2 | 226 | 12/26/2023 |
2.1.1.1 | 123 | 12/26/2023 |
2.1.1 | 136 | 12/25/2023 |
2.1.0 | 150 | 12/17/2023 |
2.0.11 | 204 | 12/6/2023 |
2.0.1 | 205 | 11/15/2023 |
2.0.0 | 157 | 11/14/2023 |
1.9.1 | 169 | 11/1/2023 |
1.9.0 | 141 | 10/19/2023 |
1.9.0-preview2 | 325 | 10/12/2023 |
1.9.0-preview1 | 109 | 10/12/2023 |
1.8.9 | 187 | 10/11/2023 |
1.8.8 | 180 | 10/11/2023 |
1.8.7-rc2 | 141 | 9/21/2023 |
1.8.7-rc1 | 130 | 9/12/2023 |
1.8.6 | 180 | 8/31/2023 |
1.8.5 | 865 | 8/25/2023 |
1.8.4 | 170 | 8/24/2023 |
1.8.3 | 192 | 8/23/2023 |
1.8.2 | 264 | 8/22/2023 |
1.8.1 | 203 | 8/18/2023 |
1.8.0 | 195 | 8/15/2023 |
1.7.9 | 228 | 8/11/2023 |
1.7.8 | 172 | 8/11/2023 |
1.7.7 | 184 | 8/10/2023 |
1.7.6 | 199 | 8/9/2023 |
1.7.5 | 250 | 8/9/2023 |
1.7.4 | 282 | 8/3/2023 |
1.7.3 | 189 | 8/1/2023 |
1.7.2 | 184 | 7/31/2023 |
1.7.1 | 181 | 7/27/2023 |
1.7.0 | 198 | 7/25/2023 |
1.6.9 | 179 | 7/25/2023 |
1.6.8 | 190 | 7/24/2023 |
1.6.7 | 211 | 7/20/2023 |
1.6.6 | 205 | 7/19/2023 |
1.6.5 | 183 | 7/19/2023 |
1.6.4 | 185 | 7/17/2023 |
1.6.3 | 161 | 7/17/2023 |
1.6.2 | 214 | 7/12/2023 |
1.6.1 | 226 | 6/30/2023 |
1.6.0 | 158 | 6/26/2023 |
1.5.9 | 174 | 6/22/2023 |
1.5.8 | 178 | 6/15/2023 |
1.5.7.1 | 179 | 6/14/2023 |
1.5.7 | 176 | 6/14/2023 |
1.5.6.2 | 231 | 6/7/2023 |
1.5.6.1 | 158 | 6/7/2023 |
1.5.6 | 158 | 6/7/2023 |
1.5.5.2 | 218 | 5/26/2023 |
1.5.5.1 | 176 | 5/26/2023 |
1.5.5 | 176 | 5/26/2023 |
1.5.4.4 | 189 | 5/25/2023 |
1.5.4.3 | 230 | 5/23/2023 |
1.5.4.2 | 292 | 5/17/2023 |
1.5.4.1 | 184 | 5/16/2023 |
1.5.4 | 261 | 5/11/2023 |
1.5.3 | 182 | 5/11/2023 |
1.5.2 | 125 | 5/10/2023 |
1.5.1 | 123 | 5/10/2023 |
1.5.0 | 164 | 5/6/2023 |
1.4.0 | 120 | 5/5/2023 |
1.3.9 | 133 | 4/23/2023 |
1.3.8.6 | 115 | 4/23/2023 |
1.3.8.5 | 125 | 4/21/2023 |
1.3.8.1 | 191 | 4/12/2023 |
1.3.8 | 130 | 4/11/2023 |
1.3.7 | 155 | 4/9/2023 |
1.3.6.3 | 212 | 4/1/2023 |
1.3.6.2 | 128 | 3/31/2023 |
1.3.6.1 | 129 | 3/31/2023 |
1.3.6 | 101 | 3/31/2023 |
1.3.5 | 126 | 3/30/2023 |
1.3.4.1 | 187 | 3/29/2023 |
1.3.4 | 134 | 3/28/2023 |
1.3.3 | 110 | 3/28/2023 |
1.3.2 | 143 | 3/26/2023 |
1.3.1 | 198 | 3/22/2023 |
1.3.0 | 137 | 3/21/2023 |
1.2.0 | 122 | 3/21/2023 |
1.1.0 | 137 | 3/17/2023 |
1.0.9 | 133 | 3/15/2023 |
1.0.8 | 135 | 3/15/2023 |
1.0.7 | 114 | 3/15/2023 |
1.0.6 | 132 | 3/13/2023 |
1.0.5 | 138 | 3/13/2023 |
1.0.4 | 114 | 3/13/2023 |
1.0.3 | 203 | 2/26/2023 |
1.0.2 | 125 | 2/26/2023 |
1.0.1 | 133 | 2/23/2023 |
1.0.0 | 294 | 2/20/2023 |