diff --git a/OptionStudy.Next/3配置绑定/ConfigBinderTest.cs b/OptionStudy.Next/3配置绑定/ConfigBinderTest.cs index f63d759..2855d24 100644 --- a/OptionStudy.Next/3配置绑定/ConfigBinderTest.cs +++ b/OptionStudy.Next/3配置绑定/ConfigBinderTest.cs @@ -1,7 +1,7 @@  using OptionStudy.UnitApp.Next; -namespace OptionStudy.Next._3配置绑定 +namespace OptionStudy.Next { /// /// 配置绑定 测试 diff --git a/OptionStudy.Next/4配置同步/ConfigSyncTest.cs b/OptionStudy.Next/4配置同步/ConfigSyncTest.cs new file mode 100644 index 0000000..cfca45c --- /dev/null +++ b/OptionStudy.Next/4配置同步/ConfigSyncTest.cs @@ -0,0 +1,125 @@ + +using OptionStudy.UnitApp.Next; + +namespace OptionStudy.Next +{ + /// + /// 配置的同步 + /// + public class ConfigSyncTest : IDisposable + { + private readonly ITestOutputHelper testOutput; + + public ConfigSyncTest(ITestOutputHelper testOutputHelper) + { + this.testOutput = testOutputHelper; + } + + /// + /// 触发同步事件 + /// Provider 执行 Load() 方法 + /// + [Fact] + public void SyncConfigFor_Provider_Load_Test() + { + var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs/appsettings.json"); + + var root = new ConfigurationBuilder() + .AddJsonFile(filePath, false, true) + .Build(); + bool excuted =false; + + Action run = (obj) => // 执行事件 + { + excuted=true; + var configJsonText = System.Text.Json.JsonSerializer.Serialize(root.Get()); + testOutput?.WriteLine(configJsonText); + }; + + //注册回调事件 + root.GetReloadToken().RegisterChangeCallback(run,null); + + //触发回调:Provider 重新加载数据 + root.Providers.First().Load(); + + //等待一会,监控到文件变化后会执行回调方法 + Thread.Sleep(500); + + //回调执行过 + Assert.True(excuted); + } + + /// + /// 触发同步事件 + /// IConfigurationRoot 执行 Reload() 方法 + /// + [Fact] + public void SyncConfigFor_IConfigurationRoot_Reload_Test() + { + var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs/appsettings.json"); + + var root = new ConfigurationBuilder() + .AddJsonFile(filePath, false, true) + .Build(); + + bool excuted = false; + Action run = (obj) => // 执行事件 + { + excuted = true; + var configJsonText = System.Text.Json.JsonSerializer.Serialize(root.Get()); + testOutput?.WriteLine(configJsonText); + }; + + //注册回调事件 + root.GetReloadToken().RegisterChangeCallback(run, null); + + //触发回调:ConfigurationRoot 重新加载 + root.Reload(); + + //等待一会,监控到文件变化后会执行回调方法 + Thread.Sleep(500); + + //回调执行过 + Assert.True(excuted); + } + + /// + /// 触发同步事件 + /// 修改物理文件内容 + /// + [Fact] + public void SyncConfigFor_ChangeFile_Test() + { + var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs/appsettings.json"); + + var root = new ConfigurationBuilder() + .AddJsonFile(filePath, false, true) + .Build(); + + bool excuted = false; + Action run = (obj) => // 执行事件 + { + excuted = true; + var configJsonText = System.Text.Json.JsonSerializer.Serialize(root.Get()); + testOutput?.WriteLine(configJsonText); + }; + + //注册回调事件 + root.GetReloadToken().RegisterChangeCallback(run, null); + + //触发回调:修改配置源文件的内容 + File.AppendAllText(filePath, " "); + + //等待一会,监控到文件变化后会执行回调方法 + Thread.Sleep(500); + + //回调执行过 + Assert.True(excuted); + } + + public void Dispose() + { + + } + } +} diff --git a/OptionStudy.Next/OptionStudy.Next.csproj b/OptionStudy.Next/OptionStudy.Next.csproj index e70c8d7..9fa8b26 100644 --- a/OptionStudy.Next/OptionStudy.Next.csproj +++ b/OptionStudy.Next/OptionStudy.Next.csproj @@ -61,7 +61,6 @@ -