配置选项 空值

问题描述 投票:1回答:1

我有一个问题,我将ConfigValidator注册为Singleton以及其他类来检查appsettings.json的有效性,但是我在ConfigValidator中获得了空值。配置即时检查,并填充值,调试进入ConfigValidator类,但将值写入null

Startup.cs:

public void ConfigureServices(IServiceCollection services)
    {

        var builder = new ConfigurationBuilder()
            .AddJsonFile(ConfigPath + Path.DirectorySeparatorChar + ConfigFile, optional: false, reloadOnChange: true)
            .AddEnvironmentVariables();

        Configuration = builder.Build();

        services.AddOptions();
        services.AddSingleton<ConfigValidator>();
        services.Configure<NginxStorageConfig>(Configuration);
        services.Configure<FormManagerConfig>(Configuration);
        services.Configure<SslCertConfig>(Configuration);
        services.Configure<WebApiConfig>(Configuration);
    }



public void Configure(IApplicationBuilder app, ConfigValidator validator)
    {
        app.UseAuthentication();
        app.UseHsts();
        app.UseMiddleware(typeof(ErrorHandlingMiddleware));
        app.UseMvc();
        validator.ValidateConfig(); //call validator 
    }

ConfigValidator:

public class ConfigValidator
{
    private readonly IOptions<FormManagerConfig> _formManager;
    private readonly IOptions<NginxStorageConfig> _ngixStorage;
    private readonly IOptions<SslCertConfig> _certConfig;
    private readonly IOptions<WebApiConfig> _webApi;

    public ConfigValidator(IOptions<FormManagerConfig> formManager,
        IOptions<NginxStorageConfig> ngixStorage,
        IOptions<SslCertConfig> certConfig,
        IOptions<WebApiConfig> webApi)
    {
        _formManager = formManager;
        _ngixStorage = ngixStorage;
        _certConfig = certConfig;
        _webApi = webApi;
    }

    public void ValidateConfig()
    {
        var portValue = _webApi.Value.Port;

        string[] str =
        {
            _formManager.Value.FormConfigurationFileName,
            _formManager.Value.PathToFormsStorage,

            _ngixStorage.Value.UploadFilePath,
            _ngixStorage.Value.NginxFileStoreLocation,
            _ngixStorage.Value.NgnixFormsStorageLocation,

            _certConfig.Value.Password,
            _certConfig.Value.Path
        };

        try
        {
            Validate(str);
            if (portValue == 0 && portValue < 0)
            {
                throw new ValidationException($"{nameof(portValue)} incorrect");
            }
            else
            {
                Instance.Logger.Info($"{nameof(portValue)} correct");
            }
        }
        catch (ValidationException e)
        {
            Instance.Logger.Error(e);
        }
    }

    private void Validate(params string[] strings)
    {
        foreach (var value in strings)
        {
            if (value.IsNullOrEmpty())
            {
                throw new ValidationException($"{nameof(value)} incorrect");
            }
            else
            {
                Instance.Logger.Info($"Value {value} correct");
            }
        }
    }
}

“值的图像”“>

我有一个问题,我将ConfigValidator注册为Singleton以及其他类来检查appsettings.json的有效性,但是我在ConfigValidator中获得了空值。配置即时检查,它是...

c# asp.net-core options
1个回答
0
投票
services.AddSingleton<ConfigValidator>();
services.Configure<NginxStorageConfig>(Configuration.GetSection("nginxStorage"));
services.Configure<FormManagerConfig>(Configuration.GetSection("form_manager"));
services.Configure<SslCertConfig>(Configuration.GetSection("SslCert"));
services.Configure<WebApiConfig>(Configuration.GetSection("webApi"));
© www.soinside.com 2019 - 2024. All rights reserved.