Azrng.ConsoleApp.DependencyInjection
1.3.5
dotnet add package Azrng.ConsoleApp.DependencyInjection --version 1.3.5
NuGet\Install-Package Azrng.ConsoleApp.DependencyInjection -Version 1.3.5
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="Azrng.ConsoleApp.DependencyInjection" Version="1.3.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Azrng.ConsoleApp.DependencyInjection" Version="1.3.5" />
<PackageReference Include="Azrng.ConsoleApp.DependencyInjection" />
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 Azrng.ConsoleApp.DependencyInjection --version 1.3.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Azrng.ConsoleApp.DependencyInjection, 1.3.5"
#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 Azrng.ConsoleApp.DependencyInjection@1.3.5
#: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=Azrng.ConsoleApp.DependencyInjection&version=1.3.5
#tool nuget:?package=Azrng.ConsoleApp.DependencyInjection&version=1.3.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Azrng.ConsoleApp.DependencyInjection
🚀 现代化的控制台应用开发框架 - 简化依赖注入配置,专注业务逻辑
📋 项目简介
控制台依赖注入扩展,为 .NET 控制台应用提供现代化的依赖注入和配置管理能力。
✨ 核心特性
- ⚙️ 支持读取 appsettings.json 配置文件
- 📝 默认使用 Microsoft.Extensions.Logging(Console + Debug + 本地文件)日志输出
- 🎯 简化的依赖注入配置
- 🔧 灵活的服务注册方式
- 🌍 支持环境配置(appsettings.{Environment}.json)
- ✅ 启用容器校验,确保依赖关系正确
构建方法
方式1: 简单方式,不需要依赖注入
var builder = new ConsoleAppServer(args);
await using var sp = builder.Build<TempService>();
await sp.RunAsync();
方式2: 注入通用配置
var builder = new ConsoleAppServer(args);
// 注入自定义配置
builder.Services.AddHttpClient();
await using var sp = builder.Build<UrlSortService>();
await sp.RunAsync();
Console.Read();
方式3: 委托方式注册服务
var builder = new ConsoleAppServer(args);
await using var sp = builder.Build<JsonTempService>(services =>
{
services.ConfigureDefaultJson();
});
await sp.RunAsync();
高级用法
自定义日志配置(ConfigureLogging)
默认启用 Console + Debug + ExtensionsLoggerProvider 三种日志 Provider。若需接入 Serilog 等第三方日志,或只保留部分 Provider,传入委托进行完全自定义:
var builder = new ConsoleAppServer(args);
// 传 null 或不传:使用默认日志 Provider(Console + Debug + ExtensionsLoggerProvider)
builder.ConfigureLogging();
// 传委托:完全自定义日志配置,委托内自行决定添加哪些 Provider
builder.ConfigureLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddConsole();
// loggingBuilder.AddSerilog(); // 接入其它日志框架
});
await using var sp = builder.Build<TempService>();
await sp.RunAsync();
注意:传入委托后默认 Provider(Console + Debug + ExtensionsLoggerProvider)不会自动添加,需在委托内自行配置。
绑定强类型选项配置(Configure)
Configure<TOption> 封装了 Services.Configure<TOption>(Configuration.GetSection(...)),避免手写样板代码:
public class MyOptions
{
public string Url { get; set; } = string.Empty;
public int Timeout { get; set; }
}
var builder = new ConsoleAppServer(args);
// 指定配置节名称
builder.Configure<MyOptions>("MyOptions");
// 或不传节名,默认以类型名 MyOptions 作为配置节
builder.Configure<MyOptions>();
await using var sp = builder.Build<TempService>();
// 在服务中注入 IOptions<MyOptions> 即可读取
版本更新记录
- 1.3.5
- 新增
ConfigureLogging(Action<ILoggingBuilder>?)委托重载,支持自定义日志 Provider(接入 Serilog 等),传 null 时保持默认行为 - 新增
Configure<TOption>便捷方法,封装Services.Configure<TOption>(Configuration.GetSection(...)),支持链式调用 - 优化
ExtensionsLogger日志分发:switch 分支改为静态字典查表,统一走LocalLogHelper.WriteMyLogs入口 - 在 csproj 通过
NoWarn抑制泛型Configure<T>的 SYSLIB1104 诊断(微软官方泛型配置绑定在 AOT 下的已知限制,见 dotnet/runtime#89273)
- 新增
- 1.3.4
ConsoleAppServer构造函数参数支持可空,默认值为nullIServiceStart.Title属性简化为隐式 public 访问修饰符- 优化代码格式
- 1.3.3
- 配置文件基路径改为
AppContext.BaseDirectory,并支持按环境加载appsettings.{Environment}.json - 优化 DI 启动流程:
IServiceStart使用作用域解析并启用容器校验(ValidateOnBuild、ValidateScopes) - 修复
ExtensionsLogger的可空签名告警并完善异常日志内容 - 文档修正:默认日志实现为
Microsoft.Extensions.Logging,不再描述为 Serilog
- 配置文件基路径改为
- 1.3.2
- 发布正式版
- 1.3.2-beta3
- 移除Serilog相关包测试
- 1.3.2-beta2
- 引用.Net10正式包
- 1.3.2-beta1
- 适配.Net10
- 1.3.1
- 适配Azrng.Core的更新
- 1.3.0
- 支持Build重载,支持针对Service注入特定配置
- 1.2.0
- 支持将默认ILogger日志输出到本地文件
- 1.1.0
- 更新依赖项
- 1.0.1
- 读取环境变量需增加变量前缀:ASPNETCORE_
- 1.1.0-beta1
- 测试进一步缩小打包文件的大小
- 1.0.0
- 基础控制台依赖注入开发
| 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. 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.
-
net10.0
- Azrng.Core (>= 1.19.0)
- Microsoft.Extensions.Hosting (>= 10.0.0)
-
net8.0
- Azrng.Core (>= 1.19.0)
- Microsoft.Extensions.Hosting (>= 8.0.0)
-
net9.0
- Azrng.Core (>= 1.19.0)
- Microsoft.Extensions.Hosting (>= 9.0.0)
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 |
|---|---|---|
| 1.3.5 | 41 | 6/24/2026 |
| 1.3.4 | 117 | 3/1/2026 |
| 1.3.3 | 113 | 2/27/2026 |
| 1.3.2 | 612 | 12/1/2025 |
| 1.3.2-beta3 | 433 | 11/19/2025 |
| 1.3.2-beta2 | 304 | 11/12/2025 |
| 1.3.2-beta1 | 176 | 11/6/2025 |
| 1.3.1 | 211 | 7/7/2025 |
| 1.3.0 | 217 | 7/1/2025 |
| 1.2.0 | 206 | 5/9/2025 |
| 1.1.0 | 191 | 3/21/2025 |
| 1.1.0-beta1 | 162 | 2/18/2025 |
| 1.0.1 | 190 | 2/23/2025 |
| 1.0.0 | 184 | 2/17/2025 |