.net core 为选项数据添加验证:避免错误配置的应用接收用户流量 电脑版发表于:2020/7/8 10:40 ![.netcore](https://img.tnblog.net/arcimg/hb/c857299a86d84ee7b26d181a31e58234.jpg ".netcore") >#.net core 为选项数据添加验证:避免错误配置的应用接收用户流量 [TOC] <br/> 三种验证方法 ------------ <br/> - 直接注册验证函数 - 实现 IValidateOptions<out TOptions> - 使用 Microsoft.Extensions.Options.DataAnnotations 内容示例 ------------ >###项目架构 参考:https://www.tnblog.net/hb/article/details/4738 >###直接注册验证函数 <br/> >通过 OrderServiceExtensions.cs 进行验证配置 MaxOrderCount 值 < 100 <font style="color:#2ecc71;font-weight:bold;">OrderServiceExtensions.cs</font> ```csharp public static IServiceCollection AddOrderService(this IServiceCollection services,IConfiguration configuration) { services.AddOptions<OrderServiceOptions>().Configure(options => { configuration.Bind(options); }).Validate(options=> { //验证逻辑 return options.MaxOrderCount < 100; }, "MaxOrderCount 不能大于 100");//报错信息 services.AddSingleton<IOrderService, OrderService>(); return services; } ``` >这里<font style="color:#f1c40f;font-weight:bold;">appsetting.json</font>的值 ```json "OrderService": { "MaxOrderCount": 200 } ``` >我们会发现它大于100肯定是不行所以: ![大于100](https://img.tnblog.net/arcimg/hb/4b2b1088587848839b9e924db9729ded.png) >###属性注入进行验证 <br/> ><font style="color:#2ecc71;font-weight:bold;">OrderServiceExtensions.cs</font> ```csharp public static IServiceCollection AddOrderService(this IServiceCollection services,IConfiguration configuration) { services.AddOptions<OrderServiceOptions>().Configure(options => { configuration.Bind(options); }).ValidateDataAnnotations(); services.AddSingleton<IOrderService, OrderService>(); return services; } ``` ><font style="color:#2ecc71;font-weight:bold;">OrderService.cs</font> ```csharp public class OrderServiceOptions { //限制只能在1-20之间 [Range(1,20)] public int MaxOrderCount { get; set; } = 100; } ``` >我们会发现它不在1-20的范围之内肯定是不行的: ![范围](https://img.tnblog.net/arcimg/hb/b6d4956f23c649c58672609863e58470.png) >###通过接口进行验证 <br/> >在 <font style="color:#2ecc71;font-weight:bold;">OrderService.cs</font> 下添加验证类 注意:验证类是需要实现 <font style="color:#f39c12;font-weight:bold;">IValidateOptions</font> 接口的,通过 <font style="color:#9b59b6;font-weight:bold;">Validate</font> 方法实现自定义验证逻辑 ```csharp public class OrderServiceValidateOptions : IValidateOptions<OrderServiceOptions> { public ValidateOptionsResult Validate(string name, OrderServiceOptions options) { if (options.MaxOrderCount > 100) { return ValidateOptionsResult.Fail("不能大于100"); } else { return ValidateOptionsResult.Success; } } } ``` >通过**单例模式**注入到<font style="color:#2ecc71;font-weight:bold;">OrderServiceExtensions.cs</font> ```csharp public static IServiceCollection AddOrderService(this IServiceCollection services,IConfiguration configuration) { services.AddOptions<OrderServiceOptions>().Configure(options => { configuration.Bind(options); }).Services.AddSingleton<IValidateOptions<OrderServiceOptions>, OrderServiceValidateOptions>(); services.AddSingleton<IOrderService, OrderService>(); return services; } ``` >运行一下: ![运行一下](https://img.tnblog.net/arcimg/hb/4bd658923e7d49cebdda82d094c66fb8.png) 总结 ------------ >通过添加选项的验证,我们可以在配置错误的情况下阻止应用程序启动,这样我们就可以避免用户流量达到错误的节点上!