Inkslab.Config 1.2.25

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

Inkslab

Inkslab.Config 是什么?

Inkslab.ConfigIConfigHelper 的默认实现,提供强类型配置读取与热更新能力,跨 .NET Framework / .NET Standard / .NET Core 一致使用。

  • .NET Framework:基于 System.ConfigurationConfigurationManager)。
  • .NET Standard / .NET Core:基于 Microsoft.Extensions.Configuration(默认包含 appsettings.jsonappsettings.{Environment}.json)。

安装

dotnet add package Inkslab.Config

Inkslab.Config 会通过 CStartup 自动向 SingletonPools 注册 IConfigHelper,无需显式代码。


快速入门

1. 读取字符串

var conn = "ConnectionStrings:Default".Config<string>();

2. 读取强类型对象

public class DatabaseOptions
{
    public string ConnectionString { get; set; }
    public int    Timeout          { get; set; }
}

var db = "Database".Config<DatabaseOptions>();

3. 指定默认值

int pageSize = "UI:PageSize".Config<int>(defaultValue: 20);

4. 监听变化(.NET Standard / .NET Core)

var helper = SingletonPools.Singleton<IConfigHelper>();
helper.OnConfigChanged += sender =>
{
    // 热更新逻辑
};

核心契约

IConfigHelper src/Inkslab/Config/IConfigHelper.cs

public interface IConfigHelper
{
#if !NET_Traditional
    /// <summary>配置变更事件(仅非 .NET Framework)。</summary>
    event Action<object> OnConfigChanged;
#endif

    /// <summary>按 key 读取,未找到时返回默认值。</summary>
    T Get<T>(string key, T defaultValue = default);
}

静态门面 ConfigHelper src/Inkslab/Config/ConfigHelper.cs

ConfigHelper.Get<string>("ConnectionStrings:Default");

扩展方法 Config<T> src/Inkslab/Extentions/StringExtensions.cs

"Section:Key".Config<int>(defaultValue: 0);

键路径规则

运行时 分隔符 示例 说明
.NET Framework / appSettings/key 默认读取 appSettings;可通过命名段读取 connectionStrings、自定义 ConfigurationSectionGroup
.NET Standard / .NET Core : Database:ConnectionString Microsoft.Extensions.Configuration 一致

.NET Framework 细则

  • 默认读取 appSettings 下的键值。
  • 读取连接串:connectionStrings/{name}(返回字符串名称)或 connectionStrings/{name}/connectionString(返回连接串值)。
  • 读取自定义 ConfigurationSectionGroup 时,目标类型必须与节点实际类型匹配,否则返回默认值。
  • 支持指定运行环境:new DefaultConfigHelper(RuntimeEnvironment.Service),枚举值 Web / Form / Service

.NET Standard / .NET Core 细则

  • 默认加载 appsettings.jsonappsettings.{ASPNETCORE_ENVIRONMENT}.json
  • 支持热更新(文件变更时触发 OnConfigChanged)。

进阶用法

自定义 JSON 配置源

实现 IJsonConfigSettings 并注入:

public class MyJsonConfigSettings : IJsonConfigSettings
{
    public void Config(IConfigurationBuilder builder)
    {
        builder.AddJsonFile("custom.json", optional: true, reloadOnChange: true);
    }
}

// 必须在 XStartup.DoStartup() 之前
SingletonPools.TryAdd<IJsonConfigSettings, MyJsonConfigSettings>();

内置便捷实现

类型 位置 作用
JsonConfigSettings src/Inkslab.Config/Settings/JsonConfigSettings.cs Action<IConfigurationBuilder> 方式快速配置
JsonPathConfigSettings src/Inkslab.Config/Settings/JsonPathConfigSettings.cs 按文件路径追加 JSON 配置

替换默认实现

public class MyConfigHelper : IConfigHelper
{
    public event Action<object> OnConfigChanged;
    public T Get<T>(string key, T defaultValue = default) { /* ... */ }
}

SingletonPools.TryAdd<IConfigHelper, MyConfigHelper>();

单元测试

参见 tests/Inkslab.Config.Tests/UnitTests.cs


常见问题

  • 配置未生效:确认在 XStartup.DoStartup() 之前完成 SingletonPools.TryAdd
  • .NET Framework 下读取为空:检查目标类型与配置节类型是否匹配。
  • 热更新未触发:仅 .NET Standard / .NET Core 的 JSON 源默认支持 reloadOnChange
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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 was computed.  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 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
1.2.25 98 6/9/2026
1.2.24 116 4/22/2026
1.2.23 130 3/27/2026
1.2.22 116 3/24/2026
1.2.20 109 3/24/2026
1.2.18 240 10/9/2025
1.2.17 236 8/15/2025
1.2.16 174 6/7/2025
1.2.14 235 11/22/2024
1.2.13 683 9/10/2024
1.2.12 244 9/10/2024
1.2.11 323 8/4/2024
1.2.10 239 7/29/2024
1.2.9 221 7/29/2024
1.2.8.5 3,179 5/15/2024
1.2.8 2,131 3/26/2024
1.2.7 289 3/1/2024
1.2.5 610 1/2/2024
1.2.4.1 394 12/20/2023
1.2.4 320 12/12/2023
Loading failed