首页
视频
资源
登录
原
.net core 选项框架:服务组件集成配置的最佳实践
4937
人阅读
2020/7/1 11:21
总访问:
2658881
评论:
0
收藏:
0
手机
分类:
.net后台框架
![.netcore](https://img.tnblog.net/arcimg/hb/c857299a86d84ee7b26d181a31e58234.jpg ".netcore") >#.net core 选项框架:服务组件集成配置的最佳实践 [TOC] #### 如何通过选项框架来处理服务和配置的关系? <br/> 特性 ------------ <br/> - 支持单例模式读取配置 - 支持快照 - 支持配置变更通知 - 支持运动时动态修改选项值 <br/> 设计原则 ------------ <br/> - 接口分离原则(ISP),我们的类不应该依赖它不使用的配置 - 关注点分离(SoC),不同组件、服务、类之间的配置不应相互依赖或耦合 <br/> 建议 ------------ <br/> - 为我们的服务设计 XXXOptions - 使用 IOptions<XXXOptions>、IOptionsSnapshort<XXXOptions>、IOptionsMonitor<XXXOptions>作为服务构造函数的参数 <br/> 项目结构 ------------ >这里我们创建的是一个WebAPI ![项目结构](https://img.tnblog.net/arcimg/hb/aad5ba56663142d0b8b771c09a102e2d.png) 通过选项注入案例 ------------ >###项目内容 <br/> ><font style="color:#2ecc71;font-weight:bold;">OrderService.cs</font> ```csharp public interface IOrderService { int ShowMaxOrderCount(); } public class OrderService : IOrderService { OrderServiceOptions _orderServiceOptions; public OrderService(OrderServiceOptions orderServiceOptions) { _orderServiceOptions = orderServiceOptions; } public int ShowMaxOrderCount() { return _orderServiceOptions.MaxOrderCount; } } public class OrderServiceOptions { public int MaxOrderCount { get; set; } = 100; } ``` ><font style="color:#2ecc71;font-weight:bold;">Startup.cs</font> ```csharp public void ConfigureServices(IServiceCollection services) { services.AddSingleton<OrderServiceOptions>(); services.AddSingleton<IOrderService, OrderService>(); services.AddControllers(); } ``` ><font style="color:#2ecc71;font-weight:bold;">WeatherForecastController.cs</font> ```csharp [HttpGet] public int Get([FromServices]IOrderService orderService) { Console.WriteLine($"orderService.ShowMaxOrderCount:{orderService.ShowMaxOrderCount()}"); return orderService.ShowMaxOrderCount(); } ``` >运行结果 ![运行结果](https://img.tnblog.net/arcimg/hb/ae3b3e58b6f34da9ad34a5f23885834a.png) 服务组件集成配置 ------------ >###修改项目内容 ><font style="color:#2ecc71;font-weight:bold;">OrderServiceExtensions.cs</font> ```csharp namespace Microsoft.Extensions.DependencyInjection { public static class OrderServiceExtensions { public static IServiceCollection AddOrderService(this IServiceCollection services,Action<OrderServiceOptions> action) { return services; } } } ``` ><font style="color:#2ecc71;font-weight:bold;">OrderService.cs</font> ```csharp public class OrderService : IOrderService { IOptions<OrderServiceOptions> _orderServiceOptions; public OrderService(IOptions<OrderServiceOptions> orderServiceOptions) { _orderServiceOptions = orderServiceOptions; } public int ShowMaxOrderCount() { return _orderServiceOptions.Value.MaxOrderCount; } } ``` ><font style="color:#2ecc71;font-weight:bold;">Startup.cs</font> ```csharp public void ConfigureServices(IServiceCollection services) { services.Configure<OrderServiceOptions>(Configuration.GetSection("OrderService")); services.AddSingleton<IOrderService, OrderService>(); services.AddControllers(); } ``` ><font style="color:#f1c40f;font-weight:bold;">appsetting.json</font> ```json { "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "OrderService": { "MaxOrderCount": 200 }, "AllowedHosts": "*" } ``` >运行结果 ![运行结果](https://img.tnblog.net/arcimg/hb/fdbcf7acb3834b039268dedf5e32cf07.png) >我们可以看见这里的值通过配置而发生了变化 其他 ------------ >内置的配置优先级高到低是: 命令行配置 环境变量配置 文件配置
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
171篇
linux
17篇
linux中cve
1篇
windows中cve
0篇
资源分享
10篇
Win32
3篇
前端
28篇
传说中的c
4篇
Xamarin
9篇
docker
15篇
容器编排
101篇
grpc
4篇
Go
15篇
yaml模板
1篇
理论
2篇
更多
Sqlserver
4篇
云产品
39篇
git
3篇
Unity
1篇
考证
2篇
RabbitMq
23篇
Harbor
1篇
Ansible
8篇
Jenkins
17篇
Vue
1篇
Ids4
18篇
istio
1篇
架构
2篇
网络
7篇
windbg
4篇
AI
18篇
threejs
2篇
人物
1篇
嵌入式
4篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
Halcon
5篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术