如何在DI设置过程中自动验证来自appSettings.json文件的配置值?

如何解决如何在DI设置过程中自动验证来自appSettings.json文件的配置值?

我在.NET Core项目的appSettings.json文件中添加了配置。为了简单起见,我以数据库设置为例。因此,在设置文件中您将拥有

{
  "Database": {
    "Host": "localhost","Port": 1234,"Database": "myDb","Username": "username","Password": "pw","EnablePooling": true
  }
}

在Startup.cs文件中配置服务时,我希望通过依赖项注入来访问那些设置。数据模型是

public class DatabaseSettings
{
    public string Host { get; set; }
    public ushort Port { get; set; }
    public string Database { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public bool EnablePooling { get; set; }
}

我以这种方式配置

private void SetupSettings(IServiceCollection services)
{
    ServiceProvider serviceProvider = services.BuildServiceProvider();
    IConfiguration configuration = serviceProvider.GetService<IConfiguration>();

    IConfigurationSection databaseConfigurationSection = configuration.GetSection("Database");
    services.Configure<DatabaseSettings>(databaseConfigurationSection);
}

最后我要验证这些设置。我知道我可以创建实现IValidateOptions接口的验证器类。

public class DatabaseSettingsValidator : IValidateOptions<DatabaseSettings>
{
    private readonly IList<string> failures;

    public DatabaseSettingsValidator()
    {
        failures = new List<string>();
    }
    
    public ValidateOptionsResult Validate(string databaseSettingsName,DatabaseSettings databaseSettings)
    {
        if (databaseSettings == null)
            failures.Add($"{databaseSettingsName} are required.");
        
        if (string.IsNullOrEmpty(databaseSettings?.Host))
            failures.Add($"{nameof(databaseSettings.Host)} must not be empty.");
        
        if (string.IsNullOrEmpty(databaseSettings?.Database))
            failures.Add($"{nameof(databaseSettings.Database)} must not be empty.");
        
        if (failures.Any())
            return ValidateOptionsResult.Fail(failures);

        return ValidateOptionsResult.Success;
    }
}

但是我是否必须创建此类并自行调用Validate方法?也许有这样的示例代码?

services.ValidateConfiguration<IOptions<DatabaseSettings>,DatabaseSettingsValidator>();

因此,您传入配置的设置和验证器以供使用。

解决方法

但是我在为两个问题苦苦挣扎:

有没有一种方法可以收集所有故障,而不是在之后返回 一?因此,您将获得故障列表,而不必进行修复 一个。

我是否必须创建此类并自行调用Validate方法? 也许有这样的示例代码?

services.ValidateConfiguration ();因此,您传入配置的设置 以及要使用的验证器。

是的,我们可以收集所有失败列表并立即显示它们,还可以创建一个包含Validate方法的类。请检查以下步骤:

首先,由于类名称为“ DatabaseSettings ”,因此最好将配置节名称设置为与类名称相同:

{
  "DatabaseSettings": {
    "Host": "localhost","Port": 1234,"Database": "myDb","Username": "username","Password": "pw","EnablePooling": true
  }
}

[注意]如果使用其他名称,则该值可能不会映射到Database Setting类,因此在验证数据时,它们都为空。

第二,使用数据注释方法将验证规则添加到模型属性中。

public class DatabaseSettings
{
    [Required]
    public string Host { get; set; }
    [Required]
    public ushort Port { get; set; }
    [Required]
    public string Database { get; set; }
    [Required]
    public string Username { get; set; }
    [Required]
    public string Password { get; set; }
    [Required]
    public bool EnablePooling { get; set; }
}

第三,创建一个ServiceCollectionExtensions类,其中包含ConfigureAndValidate方法:

public static class ServiceCollectionExtensions
{
    public static IServiceCollection ConfigureAndValidate<T>(this IServiceCollection @this,IConfiguration config) where T : class
        => @this
            .Configure<T>(config.GetSection(typeof(T).Name))
            .PostConfigure<T>(settings =>
            {
                var configErrors = settings.ValidationErrors().ToArray();
                if (configErrors.Any())
                {
                    var aggrErrors = string.Join(",",configErrors);
                    var count = configErrors.Length;
                    var configType = typeof(T).Name;
                    throw new ApplicationException(
                        $"Found {count} configuration error(s) in {configType}: {aggrErrors}");
                }
            });
}

然后,注册ConfigureAndValidate服务:

public void ConfigureServices(IServiceCollection services)
{
    services.ConfigureAndValidate<DatabaseSettings>(Configuration);
}

最后,获取“例外”列表。

public class HomeController : Controller
{
    private readonly DatabaseSettings_settings;

    public HomeController(IOptions<DatabaseSettings> settings)
    {
        _settings = settings.Value; // <-- FAIL HERE THROW EXCEPTION
    }
}

然后,这样的测试结果(我从appSettings.json中删除了主机和用户名):

enter image description here

更多详细信息,您可以查看以下博客:Validating configuration in ASP.NET Core

,

ValidateOptions主要用于复杂的情况,使用ValidateOptions的目的是可以将验证逻辑从启动中移出。

我认为对于您的情况,您可以使用以下代码作为参考

public void ConfigureServices(IServiceCollection services)
{
    services.AddOptions<MyConfigOptions>()
        .Bind(Configuration.GetSection(MyConfigOptions.MyConfig))
        .ValidateDataAnnotations()
        .Validate(config =>
        {
            if (config.Key2 != 0)
            {
                return config.Key3 > config.Key2;
            }

            return true;
        },"Key3 must be > than Key2.");   // Failure message.

    services.AddControllersWithViews();
}

有关更多详细信息,请参阅此文档 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1#options-validation

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-