.Net Core实现选择数据热更新让处事感知设置的变

当一些设置需要修改在举办获取时,凡是做法是修改完设置文件后再重启web处事器可能docker举办完成,下面我先容一种热更新要领,修改完设置文件后,不需要重启处事器即可获取最新的设置文件,让处事感知设置的变革。

2、实践

下面我通过二种方法来讲授一下.Net Core实现选择数据热更新,让处事感知设置的变革。

2.1 通过AddSingleton单例方法注入,然后利用 IOptionsMonitor实现数据热更新

2.1.1 首先在Startup.cs文件中的ConfigureServices要领添加设置

//通过读取设置文件加载到SystemPath类中 services.Configure<SystemPath>(Configuration.GetSection("SystemPath")); //添加处事注入 services.AddSingleton<IPathService, PathService>();

public class SystemPath { public string FilePath { get; set; } }

2.1.2 在PathService结构器中注入IOptionsMonitor<SystemPath>实现数据热更新

public class PathService : IPathService { IOptionsMonitor<SystemPath> _options; /// <summary> /// 结构函数 /// </summary> /// <param></param> public PathService(IOptionsMonitor<SystemPath> options) { _options = options; } public string GetPath() { return _options.CurrentValue.FilePath; } }

2.1.3 在PathController中通过挪用接口方法读取最新设置路径

/// <summary> /// 路径 /// </summary> [Route("api/[controller]/[action]")] [ApiController] public class PathController : ControllerBase { private readonly IPathService _pathService; /// <summary> /// 结构函数 /// </summary> /// <param></param> public PathController(IPathService pathService) { _pathService = pathService; } /// <summary> /// 获取系统路径 /// </summary> /// <returns></returns> [HttpGet] public MethodResult GetSystemPath() { return new MethodResult(_pathService.GetPath()); } }

运行看一下结果:

.Net Core实现选择数据热更新让办事感知配置的变

此刻读取到的路径是D:/File/2.jpg,我们修改一下设置文件然后从头挪用接口看一下,这时会更新最新的路径。

 

.Net Core实现选择数据热更新让办事感知配置的变

2.2 通过AddScoped 方法注入,然后利用 IOptionsSnapshot 实现数据热更新

2.2.1 首先在Startup.cs文件中的ConfigureServices要领添加设置

//通过读取设置文件加载到SystemPath类中 services.Configure<SystemPath>(Configuration.GetSection("SystemPath")); //添加处事注入 services.AddScoped<IPathService, PathService>();

public class SystemPath { public string FilePath { get; set; } }

2.2.2 在PathService结构器中注入IOptionsMonitor<SystemPath>实现数据热更新

public class PathService : IPathService { IOptionsSnapshot<SystemPath> _options; /// <summary> /// 结构函数 /// </summary> /// <param></param> public PathService(IOptionsSnapshot<SystemPath> options) { _options = options; } public string GetPath() { return _options.Value.FilePath; } }

2.2.3 在PathController中通过挪用接口方法读取最新设置路径

/// <summary> /// 路径 /// </summary> [Route("api/[controller]/[action]")] [ApiController] public class PathController : ControllerBase { private readonly IPathService _pathService; /// <summary> /// 结构函数 /// </summary> /// <param></param> public PathController(IPathService pathService) { _pathService = pathService; } /// <summary> /// 获取系统路径 /// </summary> /// <returns></returns> [HttpGet] public MethodResult GetSystemPath() { return new MethodResult(_pathService.GetPath()); } }

运行看一下结果:

.Net Core实现选择数据热更新让办事感知配置的变

此刻读取到的路径是D:/File/2.jpg,我们修改一下设置文件然后从头挪用接口看一下,这时会更新最新的路径。

 

.Net Core实现选择数据热更新让办事感知配置的变

到此这篇关于.Net Core实现选择数据热更新让处事感知设置的变革的文章就先容到这了,更多相关.Net Core数据热更新内容请搜索剧本之家以前的文章或继承欣赏下面的相关文章但愿各人今后多多支持剧本之家!

您大概感乐趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsjyfs.html