From 489a0b6fb8f9f281a3143753266d7249154df4d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= Date: Thu, 15 Sep 2022 17:09:30 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E7=83=AD?= =?UTF-8?q?=E9=87=8D=E8=BD=BD=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Yi.Framework.ApiMicroservice/Program.cs | 2 + .../BuilderExtend/JsonFileExtension.cs | 4 +- .../FileOptionsWritableBase.cs | 14 +++++++ .../OptionsWritable/IOptionsWritable.cs | 10 +++++ .../Internal/JsonOptionsWritable.cs | 37 +++++++++++++++++ .../OptionsWritable/OptionsWritableBase.cs | 29 +++++++++++++ ...onfigurationServiceCollectionExtensions.cs | 41 +++++++++++++++++++ .../Yi.Framework.WebCore.csproj | 4 ++ 8 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/FileOptionsWritableBase.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/IOptionsWritable.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/Internal/JsonOptionsWritable.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/OptionsWritableBase.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/OptionsWritableConfigurationServiceCollectionExtensions.cs diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs index 52e69fb6..33507d35 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs @@ -10,6 +10,8 @@ using Microsoft.Extensions.Localization; using Yi.Framework.WebCore.AttributeExtend; using Yi.Framework.WebCore.SignalRHub; + + var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddCommandLine(args); builder.WebHost.UseUrls(builder.Configuration.GetValue("StartUrl")); diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/JsonFileExtension.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/JsonFileExtension.cs index 9a3c0c73..b5b67a2a 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/JsonFileExtension.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/JsonFileExtension.cs @@ -16,14 +16,14 @@ namespace Yi.Framework.WebCore.BuilderExtend string[] myJsonFile = new string[] { "appsettings.json", "Config/configuration.json" }; foreach (var item in myJsonFile) { - builder.AddJsonFile(item, optional: true, reloadOnChange: false); + builder.AddJsonFile(item, optional: true, reloadOnChange: true); } } else { foreach (var item in JsonFile) { - builder.AddJsonFile(item, optional: true, reloadOnChange: false); + builder.AddJsonFile(item, optional: true, reloadOnChange: true); } } diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/FileOptionsWritableBase.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/FileOptionsWritableBase.cs new file mode 100644 index 00000000..2cc18721 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/FileOptionsWritableBase.cs @@ -0,0 +1,14 @@ + +using Microsoft.Extensions.Options; + +namespace Yi.Framework.WebCore.BuilderExtend.OptionsWritable; + +public abstract class FileOptionsWritableBase : OptionsWritableBase, IOptionsWritable where TOptions : class, new() +{ + public FileOptionsWritableBase(IOptionsMonitor options, string section, string fileName) : base(options, section) + { + this.FileName = fileName; + } + + public string FileName { get; } +} \ No newline at end of file diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/IOptionsWritable.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/IOptionsWritable.cs new file mode 100644 index 00000000..e0816b55 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/IOptionsWritable.cs @@ -0,0 +1,10 @@ + +using Microsoft.Extensions.Options; +using System; + +namespace Yi.Framework.WebCore.BuilderExtend.OptionsWritable; + +public interface IOptionsWritable : IOptionsSnapshot where TOptions : class, new() +{ + void Update(Action configuration); +} \ No newline at end of file diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/Internal/JsonOptionsWritable.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/Internal/JsonOptionsWritable.cs new file mode 100644 index 00000000..47ee93ff --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/Internal/JsonOptionsWritable.cs @@ -0,0 +1,37 @@ +using Microsoft.Extensions.Options; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.IO; + +namespace Yi.Framework.WebCore.BuilderExtend.OptionsWritable.Internal; + +internal class JsonOptionsWritable : FileOptionsWritableBase, IOptionsWritable where TOptions : class, new() +{ + public JsonOptionsWritable(IOptionsMonitor options, string section, string fileName) : base(options, section, fileName) + { + } + + public override void Update(Action configuration) + { + JObject? jObject = JsonConvert.DeserializeObject(File.ReadAllText(this.FileName)); + if (jObject != null) + { + TOptions option = this.Monitor.CurrentValue ?? new TOptions(); + + if (jObject.TryGetValue(this.Section, out JToken? jtoken)) + { + option = JsonConvert.DeserializeObject(jtoken.ToString()) ?? new TOptions(); + configuration?.Invoke(option); + jObject[this.Section] = JObject.Parse(JsonConvert.SerializeObject(option)); + } + else + { + configuration?.Invoke(option); + jObject.TryAdd(this.Section, JObject.Parse(JsonConvert.SerializeObject(option))); + } + File.WriteAllText(this.FileName, JsonConvert.SerializeObject(jObject, Formatting.Indented)); + } + } +} \ No newline at end of file diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/OptionsWritableBase.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/OptionsWritableBase.cs new file mode 100644 index 00000000..e1147e94 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/OptionsWritableBase.cs @@ -0,0 +1,29 @@ + +using Microsoft.Extensions.Options; +using System; + +namespace Yi.Framework.WebCore.BuilderExtend.OptionsWritable; + +public abstract class OptionsWritableBase : IOptionsWritable where TOptions : class, new() +{ + public OptionsWritableBase( + IOptionsMonitor options, + string section) + { + this.Monitor = options; + this.Section = section; + } + + public IOptionsMonitor Monitor { get; } + + public string Section { get; } + + public TOptions Value => this.Monitor.CurrentValue ?? new TOptions(); + + public TOptions Get(string name) + { + return this.Monitor.Get(name); + } + + public abstract void Update(Action configuration); +} \ No newline at end of file diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/OptionsWritableConfigurationServiceCollectionExtensions.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/OptionsWritableConfigurationServiceCollectionExtensions.cs new file mode 100644 index 00000000..d6d761d9 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/OptionsWritable/OptionsWritableConfigurationServiceCollectionExtensions.cs @@ -0,0 +1,41 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using System; +using System.Configuration; +using Yi.Framework.WebCore.BuilderExtend.OptionsWritable; +using Yi.Framework.WebCore.BuilderExtend.OptionsWritable.Internal; + +namespace Yi.Framework.WebCore.BuilderExtend; + +public static class OptionsWritableConfigurationServiceCollectionExtensions +{ + public static void ConfigureJson(this IServiceCollection services, Microsoft.Extensions.Configuration.ConfigurationManager configuration, string jsonFilePath) + where TOption : class, new() + { + ConfigureJson(services, configuration.GetSection(typeof(TOption).Name), jsonFilePath); + } + + public static void ConfigureJson(this IServiceCollection services, IConfigurationSection section, string jsonFilePath) + where TOption : class, new() + { + services.Configure(section); + services.AddTransient>(provider => + new JsonOptionsWritable(provider.GetRequiredService>(), section.Key, jsonFilePath)); + } + + public static void ConfigureJson(this IServiceCollection services, Microsoft.Extensions.Configuration.ConfigurationManager configuration, Func jsonFilePathFunc) + where TOption : class, new() + { + ConfigureJson(services, configuration.GetSection(typeof(TOption).Name), jsonFilePathFunc); + } + + public static void ConfigureJson(this IServiceCollection services, IConfigurationSection section, Func jsonFilePathFunc) + where TOption : class, new() + { + services.Configure(section); + + services.AddTransient>(provider => + new JsonOptionsWritable(provider.GetRequiredService>(), section.Key, jsonFilePathFunc.Invoke(provider))); + } +} \ No newline at end of file diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj b/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj index 1a6baaf2..bc7336af 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj @@ -37,4 +37,8 @@ + + + +