A:配置读取
This commit is contained in:
1 parent
859814514b
commit
727c83da2d
13 files changed
+671
-32
No files matched your search
@@ -12,6 +12,7 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Xml" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Ini" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="8.0.0" />
|
||||
<PackageReference Include="Tomlyn" Version="0.16.2" />
|
||||
<PackageReference Include="Tomlyn.Extensions.Configuration" Version="1.0.0" />
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
|
||||
namespace Sharp.Config;
|
||||
namespace Config;
|
||||
|
||||
public static class ConfigBuilder
|
||||
{
|
||||
@@ -37,4 +37,3 @@ public static class ConfigBuilder
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace Sharp.Config;
|
||||
namespace Config;
|
||||
|
||||
public enum ConfigFormat
|
||||
{
|
||||
@@ -7,4 +7,3 @@ public enum ConfigFormat
|
||||
Xml,
|
||||
Toml
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@ using System.Text;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Sharp.Config.Serialization;
|
||||
using Config.Serialization;
|
||||
using Tomlyn.Extensions.Configuration;
|
||||
|
||||
namespace Sharp.Config;
|
||||
namespace Config;
|
||||
|
||||
public sealed class ConfigManager
|
||||
{
|
||||
@@ -47,7 +47,8 @@ public sealed class ConfigManager
|
||||
builder.AddXmlFile(src.Path, src.Optional, options.ReloadOnChange);
|
||||
break;
|
||||
case ConfigFormat.Toml:
|
||||
builder.AddToml(src.Path, src.Optional, options.ReloadOnChange);
|
||||
// Tomlyn.Extensions.Configuration uses AddTomlFile in recent versions
|
||||
builder.AddTomlFile(src.Path, src.Optional, options.ReloadOnChange);
|
||||
break;
|
||||
default:
|
||||
throw new NotSupportedException($"Unsupported format: {src.Format}");
|
||||
@@ -154,8 +155,8 @@ public sealed class ConfigManager
|
||||
// Reload notifications
|
||||
public IDisposable OnReload(Action callback)
|
||||
{
|
||||
IChangeToken token = Configuration.GetReloadToken();
|
||||
return ChangeToken.OnChange(() => token, callback);
|
||||
// 始终基于最新的 ReloadToken 订阅,以支持连续热更新
|
||||
return ChangeToken.OnChange(() => Configuration.GetReloadToken(), callback);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,4 +181,3 @@ public sealed class ConfigurationValidationException : Exception
|
||||
Errors = errors;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
|
||||
namespace Sharp.Config;
|
||||
namespace Config;
|
||||
|
||||
public sealed class ConfigOptions
|
||||
{
|
||||
public string? BasePath { get; init; }
|
||||
public string? BasePath { get; set; }
|
||||
public IList<ConfigSource> Sources { get; } = new List<ConfigSource>();
|
||||
|
||||
public bool ReloadOnChange { get; init; } = true;
|
||||
public bool IncludeEnvironmentVariables { get; init; } = true;
|
||||
public string? EnvironmentPrefix { get; init; }
|
||||
public bool ReloadOnChange { get; set; } = true;
|
||||
public bool IncludeEnvironmentVariables { get; set; } = true;
|
||||
public string? EnvironmentPrefix { get; set; }
|
||||
|
||||
public IFileProvider? FileProvider { get; init; }
|
||||
public IFileProvider? FileProvider { get; set; }
|
||||
|
||||
// Hook: allow external modules (如 Common) 自定义构建
|
||||
public Func<IConfigurationBuilder, IConfigurationBuilder>? ConfigureBuilderHook { get; init; }
|
||||
public Func<IConfigurationBuilder, IConfigurationBuilder>? ConfigureBuilderHook { get; set; }
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Sharp.Config;
|
||||
namespace Config;
|
||||
|
||||
public sealed class ConfigSource
|
||||
{
|
||||
@@ -28,4 +28,3 @@ public sealed class ConfigSource
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Sharp.Config.Serialization;
|
||||
namespace Config.Serialization;
|
||||
|
||||
public interface IConfigSerializer
|
||||
{
|
||||
@@ -9,4 +9,3 @@ public interface IConfigSerializer
|
||||
void SerializeToFile<T>(string path, T instance, bool indented = true, Encoding? encoding = null);
|
||||
T? DeserializeFromFile<T>(string path, Encoding? encoding = null);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Sharp.Config.Serialization;
|
||||
namespace Config.Serialization;
|
||||
|
||||
public sealed class IniConfigSerializer : IConfigSerializer
|
||||
{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Sharp.Config.Serialization;
|
||||
namespace Config.Serialization;
|
||||
|
||||
public sealed class JsonConfigSerializer : IConfigSerializer
|
||||
{
|
||||
@@ -41,4 +41,3 @@ public sealed class JsonConfigSerializer : IConfigSerializer
|
||||
return Deserialize<T>(text);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
using System.Text;
|
||||
using Tomlyn;
|
||||
|
||||
namespace Sharp.Config.Serialization;
|
||||
namespace Config.Serialization;
|
||||
|
||||
public sealed class TomlConfigSerializer : IConfigSerializer
|
||||
{
|
||||
public string Serialize<T>(T instance, bool indented = true, Encoding? encoding = null)
|
||||
{
|
||||
// Tomlyn writes with indentation by default
|
||||
return Toml.FromModel(instance!, new TomlModelOptions { ConvertIndentation = true });
|
||||
return Toml.FromModel(instance!);
|
||||
}
|
||||
|
||||
public T? Deserialize<T>(string content)
|
||||
{
|
||||
return Toml.ToModel<T>(content);
|
||||
// To keep dependencies light and constraints simple across value types,
|
||||
// prefer using the configuration builder + binder path for TOML.
|
||||
// Direct generic mapping with Toml.ToModel<T> requires T : class.
|
||||
throw new NotSupportedException("Use ConfigManager with AddTomlFile + binder for typed TOML binding.");
|
||||
}
|
||||
|
||||
public void SerializeToFile<T>(string path, T instance, bool indented = true, Encoding? encoding = null)
|
||||
@@ -30,4 +32,3 @@ public sealed class TomlConfigSerializer : IConfigSerializer
|
||||
return Deserialize<T>(text);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace Sharp.Config.Serialization;
|
||||
namespace Config.Serialization;
|
||||
|
||||
public sealed class XmlConfigSerializer : IConfigSerializer
|
||||
{
|
||||
@@ -36,4 +36,3 @@ public sealed class XmlConfigSerializer : IConfigSerializer
|
||||
return Deserialize<T>(text);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user