![]() |
VOOZH | about |
dotnet add package BugFree.Configuration --version 1.0.250901.1420
NuGet\Install-Package BugFree.Configuration -Version 1.0.250901.1420
<PackageReference Include="BugFree.Configuration" Version="1.0.250901.1420" />
<PackageVersion Include="BugFree.Configuration" Version="1.0.250901.1420" />Directory.Packages.props
<PackageReference Include="BugFree.Configuration" />Project file
paket add BugFree.Configuration --version 1.0.250901.1420
#r "nuget: BugFree.Configuration, 1.0.250901.1420"
#:package BugFree.Configuration@1.0.250901.1420
#addin nuget:?package=BugFree.Configuration&version=1.0.250901.1420Install as a Cake Addin
#tool nuget:?package=BugFree.Configuration&version=1.0.250901.1420Install as a Cake Tool
👁 .NET Version
👁 Platform
👁 License
BugFree.Configuration 是一个强大而灵活的 .NET 配置管理库,支持多种配置格式(JSON、XML、INI、YAML),提供类型安全的配置访问、热更新、加密存储等企业级功能。
# 通过 NuGet 安装(如果已发布)
Install-Package BugFree.Configuration
# 或者作为项目引用
<ProjectReference Include="path\to\BugFree.Configuration\BugFree.Configuration.csproj" />
using BugFree.Configuration;
using System.ComponentModel;
// JSON 配置示例
[Config("AppSettings", ConfigProviderType.Json, "./config")]
public class AppConfig : Config<AppConfig>
{
[Description("应用程序名称")]
public string AppName { get; set; } = "MyApp";
[Description("服务器端口")]
public int Port { get; set; } = 8080;
[Description("是否启用调试模式")]
public bool Debug { get; set; } = false;
[Description("数据库连接字符串")]
public string ConnectionString { get; set; } = "Server=.;Database=MyDB;";
protected override void OnLoaded()
{
// 配置加载完成后的回调
if (IsNew)
{
Console.WriteLine("首次创建配置文件");
}
}
}
// 复杂配置示例
[Config("DatabaseConfig", ConfigProviderType.Xml, "./config")]
public class DatabaseConfig : Config<DatabaseConfig>
{
[Description("数据库设置")]
public DatabaseSettings Database { get; set; } = new();
[Description("连接池设置")]
public List<ConnectionPool> Pools { get; set; } = new();
}
public class DatabaseSettings
{
[Description("主机地址")]
public string Host { get; set; } = "localhost";
[Description("端口号")]
public int Port { get; set; } = 5432;
[Description("数据库名")]
public string Database { get; set; } = "mydb";
}
public class ConnectionPool
{
[Description("连接池名称")]
public string Name { get; set; } = "";
[Description("最大连接数")]
public int MaxConnections { get; set; } = 10;
}
using BugFree.Configuration;
class Program
{
static void Main()
{
// 访问配置(自动加载/创建)
var config = AppConfig.Current;
Console.WriteLine($"应用名称: {config.AppName}");
Console.WriteLine($"端口: {config.Port}");
Console.WriteLine($"调试模式: {config.Debug}");
// 修改配置
config.Port = 9090;
config.Debug = true;
// 保存配置
config.Save();
// 配置会自动保存到 ./config/AppSettings.json
/*
{
"AppName": "MyApp",
"Port": 9090,
"Debug": true,
"ConnectionString": "Server=.;Database=MyDB;"
}
*/
}
}
class HotReloadExample
{
static void Main()
{
var config = AppConfig.Current;
Console.WriteLine($"初始端口: {config.Port}");
// 程序运行期间,外部修改 ./config/AppSettings.json
// 配置会自动重新加载
Console.WriteLine("等待配置文件修改...");
while (true)
{
Thread.Sleep(1000);
// 每次访问 Current 都会返回最新的配置
var currentPort = AppConfig.Current.Port;
Console.WriteLine($"当前端口: {currentPort}");
}
}
}
// 启用加密的配置类
[Config("SecureConfig", ConfigProviderType.Json, "./config",
IsEncrypted = true, Secret = "your-secret-key-32-chars-long")]
public class SecureConfig : Config<SecureConfig>
{
[Description("API密钥")]
public string ApiKey { get; set; } = "";
[Description("数据库密码")]
public string DatabasePassword { get; set; } = "";
}
// 使用方式与普通配置相同
var secureConfig = SecureConfig.Current;
secureConfig.ApiKey = "sk-1234567890abcdef";
secureConfig.Save();
// 文件内容已加密,无法直接读取
[Config("MyConfig", ConfigProviderType.Json)]
public class JsonConfig : Config<JsonConfig>
{
public string Name { get; set; } = "default";
public List<string> Items { get; set; } = new() { "item1", "item2" };
public Dictionary<string, object> Settings { get; set; } = new();
}
生成的文件 (MyConfig.json):
{
"Name": "default",
"Items": [
"item1",
"item2"
],
"Settings": {}
}
[Config("MyConfig", ConfigProviderType.Xml)]
public class XmlConfig : Config<XmlConfig>
{
public string Name { get; set; } = "default";
public List<Item> Items { get; set; } = new();
// Dictionary 在 XML 中需要特殊处理
[XmlIgnore]
public Dictionary<string, string> Settings { get; set; } = new();
}
public class Item
{
public string Value { get; set; } = "";
public int Priority { get; set; } = 0;
}
生成的文件 (MyConfig.xml):
<?xml version="1.0" encoding="utf-8"?>
<XmlConfig>
<Name>default</Name>
<Items />
</XmlConfig>
[Config("MyConfig", ConfigProviderType.Ini)]
public class IniConfig : Config<IniConfig>
{
// INI 只支持基础类型
public string Name { get; set; } = "default";
public int Port { get; set; } = 8080;
public bool Enabled { get; set; } = true;
public DateTime LastUpdate { get; set; } = DateTime.Now;
}
生成的文件 (MyConfig.ini):
[IniConfig]
Name=default
Port=8080
Enabled=True
LastUpdate=2024-08-16T10:30:45.1234567
[Config("MyConfig", ConfigProviderType.Yaml)]
public class YamlConfig : Config<YamlConfig>
{
public string Name { get; set; } = "default";
public ServerConfig Server { get; set; } = new();
public List<DatabaseConfig> Databases { get; set; } = new();
}
public class ServerConfig
{
public string Host { get; set; } = "localhost";
public int Port { get; set; } = 8080;
}
生成的文件 (MyConfig.yaml):
Name: default
Server:
Host: localhost
Port: 8080
Databases: []
[Config(
name: "ConfigName", // 配置文件名(不含扩展名)
provider: ConfigProviderType.Json, // 提供者类型
path: "./config", // 配置目录路径
isencrypted: false, // 是否加密
secret: "encryption-key" // 加密密钥(32字符)
)]
| 类型 | 扩展名 | 特点 | 适用场景 |
|---|---|---|---|
ConfigProviderType.Json |
.json | 现代、灵活、支持复杂对象 | Web应用、微服务 |
ConfigProviderType.Xml |
.xml | 传统、结构化、工具支持好 | 企业应用、遗留系统 |
ConfigProviderType.Ini |
.ini | 简单、轻量、人类可读 | 系统配置、简单应用 |
ConfigProviderType.Yaml |
.yaml | 人类友好、层次清晰 | DevOps、配置管理 |
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Config<T> │───▶│ ConfigProvider │───▶│ Provider实现 │
│ (泛型基类) │ │ (抽象基类) │ │ Json/Xml/Ini... │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
▼ ▼ ▼
类型安全访问 统一生命周期管理 格式特定序列化
热更新支持 加密/解密处理 文件读写操作
线程安全保障 文件监控机制 错误处理恢复
ConfigAttributeOnLoaded() 方法// 使用环境变量控制配置路径
var configPath = Environment.GetEnvironmentVariable("CONFIG_PATH") ?? "./config";
[Config("MyApp", ConfigProviderType.Json, configPath)]
public class AppConfig : Config<AppConfig>
{
// ...
}
public class EnvironmentConfig : Config<EnvironmentConfig>
{
public string Environment { get; set; } = "Development";
protected override void OnLoaded()
{
if (IsNew)
{
// 根据环境变量设置默认值
Environment = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development";
Save();
}
}
}
public class ValidatedConfig : Config<ValidatedConfig>
{
private int _port = 8080;
public int Port
{
get => _port;
set
{
if (value < 1 || value > 65535)
throw new ArgumentOutOfRangeException(nameof(Port), "端口必须在 1-65535 范围内");
_port = value;
}
}
protected override void OnLoaded()
{
// 配置加载后验证
if (Port < 1 || Port > 65535)
{
Console.WriteLine($"警告:端口配置无效 ({Port}),重置为默认值");
Port = 8080;
Save();
}
}
}
[Test]
public void ConfigurationTest()
{
// 清理测试环境
var configPath = "./test-config/TestConfig.json";
if (File.Exists(configPath))
File.Delete(configPath);
// 重置静态实例
TestConfig.Current = null!;
// 测试首次加载
var config = TestConfig.Current;
Assert.IsTrue(config.IsNew);
Assert.AreEqual("default", config.Name);
// 测试保存和重载
config.Name = "modified";
config.Save();
TestConfig.Current = null!;
var reloaded = TestConfig.Current;
Assert.IsFalse(reloaded.IsNew);
Assert.AreEqual("modified", reloaded.Name);
}
[Config("TestConfig", ConfigProviderType.Json, "./test-config")]
public class TestConfig : Config<TestConfig>
{
public string Name { get; set; } = "default";
}
| 配置大小 | JSON | XML | INI | YAML |
|---|---|---|---|---|
| 小 (< 1KB) | 0.5ms | 1.2ms | 0.3ms | 0.8ms |
| 中 (10KB) | 2.1ms | 5.4ms | 1.8ms | 3.2ms |
| 大 (100KB) | 18ms | 45ms | 15ms | 28ms |
// 使用强密钥(32字符)
[Config("Secure", ConfigProviderType.Json, IsEncrypted = true,
Secret = "0123456789ABCDEF0123456789ABCDEF")]
public class SecureConfig : Config<SecureConfig>
{
public string ApiKey { get; set; } = "";
}
# Linux/macOS 设置配置文件权限
chmod 600 ./config/*.json
# Windows 使用 icacls
icacls .\config\*.json /grant:r %USERNAME%:F /inheritance:r
// 从环境变量读取密钥
public static class ConfigSecrets
{
public static string GetEncryptionKey()
{
return Environment.GetEnvironmentVariable("CONFIG_ENCRYPTION_KEY")
?? throw new InvalidOperationException("未设置加密密钥");
}
}
// 在配置类中使用
[Config("Secure", ConfigProviderType.Json, IsEncrypted = true)]
public class SecureConfig : Config<SecureConfig>
{
// 通过代码设置密钥而非特性参数
static SecureConfig()
{
// 这需要修改 ConfigProvider 以支持运行时密钥设置
}
}
A: 系统会创建一个新的配置实例,使用属性的默认值,并调用 OnLoaded() 方法。如果文件不存在,则会新创建文件。
A: 检查以下几点:
A: 创建新的配置类并在 OnLoaded() 中处理:
public class MigratedConfig : Config<MigratedConfig>
{
public int Version { get; set; } = 2;
public string NewProperty { get; set; } = "";
protected override void OnLoaded()
{
if (Version < 2)
{
// 执行迁移逻辑
MigrateFromV1();
Version = 2;
Save();
}
}
private void MigrateFromV1()
{
// 迁移逻辑
}
}
A: 系统会捕获序列化异常并返回新的配置实例:
protected override void OnLoaded()
{
if (IsNew)
{
// 可能是首次创建或文件损坏
Console.WriteLine("使用默认配置");
}
}
欢迎贡献代码!请遵循以下步骤:
git checkout -b feature/AmazingFeature)dotnet test)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)本项目采用 MIT 许可证 - 查看 文件了解详情。
如果您遇到问题或有建议,请:
⭐ 如果这个项目对您有帮助,请给个 Star!
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 2 NuGet packages that depend on BugFree.Configuration:
| Package | Downloads |
|---|---|
|
BugFree.FileStorage
Package Description |
|
|
BugFree.XCode.Extensions
NewLife.XCode ORM 扩展库,提供 BugFree 实体基类、数据模型接口、数据库连接及配置集成。 |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.2.2026.616-beta0953 | 67 | 6/16/2026 |
| 1.2.2026.615-beta1053 | 93 | 6/15/2026 |
| 1.2.2026.614-beta1936 | 90 | 6/14/2026 |
| 1.2.2026.613-beta1556 | 96 | 6/13/2026 |
| 1.1.2026.121-beta1102 | 149 | 1/21/2026 |
| 1.1.2026.115-beta1530 | 136 | 1/15/2026 |
| 1.0.250901.1420 | 271 | 9/1/2025 |
| 1.0.250817.1009 | 196 | 8/17/2025 |
| 1.0.250817.1003 | 194 | 8/17/2025 |
| 1.0.250817.955-beta0955 | 188 | 8/17/2025 |
| 1.0.2026.115-beta1414 | 118 | 1/15/2026 |
| 1.0.2026.106-beta1135 | 134 | 1/6/2026 |
| 1.0.2025.1224-beta1648 | 214 | 12/24/2025 |
| 1.0.2025.1224-beta1406 | 217 | 12/24/2025 |
| 1.0.2025.1224-beta1342 | 206 | 12/24/2025 |
新增 Yaml 提供者;支持可选加密(依赖 BugFree.Security);原子写入保存;统一强类型 API(Config<T>.Current/Save)。