Exemple #1
0
        static IHostBuilder CreateHostBuilder(string[] args)
        {
            var hostOut =
                Host.CreateDefaultBuilder()
                .ConfigureAppConfiguration(
                    (host_build_context, confBuilder) => {
                confBuilder.Sources.Clear();
                confBuilder.AddJsonFile("json1.json");
            }
                    );

            hostOut.ConfigureServices(
                (host_context, services) =>
            {
                services.Configure <SettingsOptions>(host_context.Configuration.GetSection(SettingsOptions.Settings));
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IValidateOptions <SettingsOptions>, ValidateSettingOptions>());

                services.AddOptions <SettingsOptions>().
                Bind(host_context.Configuration.GetSection(SettingsOptions.Settings)).ValidateDataAnnotations()
                .Validate(
                    (setOptions) => {
                    if (setOptions.Scale != 0)
                    {
                        return(setOptions.VerbosityLevel > setOptions.Scale);
                    }
                    else
                    {
                        return(true);
                    }
                }, @"Verbosity isn't largert than Scale."
                    );

                var SerProvider = services.BuildServiceProvider();

                var i = SerProvider.GetRequiredService <IOptions <SettingsOptions> >();
                try
                {
                    SettingsOptions options = i.Value;
                }
                catch (OptionsValidationException ex)
                {
                    foreach (string exp in ex.Failures)
                    {
                        Console.WriteLine($"There is \n{exp}\n");
                    }
                }
            }
                );
            return(hostOut);
        }
Exemple #2
0
        public ValidationService(IOptions <SettingsOptions> config, ILogger <ValidationService> logger)
        {
            _config = config;
            _logger = logger;

            try
            {
                SettingsOptions options = config.Value;
            }
            catch (OptionsValidationException ex)
            {
                foreach (string exp in ex.Failures)
                {
                    _logger.LogError(exp);
                }
            }
        }