コード例 #1
0
        public void ValidateTest()
        {
            var            services      = new ServiceCollection();
            var            sp            = services.BuildServiceProvider();
            IConfiguration Configuration = OptionExtensions.InitConfiguration();

            services.AddOptions();
            services
            .AddOptions <KafkaOptions>()
            .Bind(Configuration.GetSection("Kafka"))
            .Validate(option =>

                      // return a bool result:
                      Validator.TryValidateObject(
                          option,
                          new ValidationContext(option),
                          new List <ValidationResult>(),
                          validateAllProperties: true),

                      // generic error message:
                      "Kafka section invalid"
                      );

            var error = Assert.ThrowsException <OptionsValidationException>(() =>
                                                                            sp.GetRequiredService <IOptionsMonitor <AnnotatedOptions> >().CurrentValue);
        }
コード例 #2
0
        public void PostConfigureTest()
        {
            var            services      = new ServiceCollection();
            var            sp            = services.BuildServiceProvider();
            IConfiguration Configuration = OptionExtensions.InitConfiguration();

            services.AddOptions();
            services
            .AddOptions <KafkaOptions>()
            .Bind(Configuration.GetSection("Kafka"))
            .PostConfigure(x =>
            {
                var validationResults = new List <ValidationResult>();
                var context           = new ValidationContext(x);
                var valid             = Validator.TryValidateObject(x, context, validationResults);
                if (valid)
                {
                    return;
                }

                var msg = string.Join("\n", validationResults.Select(r => r.ErrorMessage));
                throw new Exception($"Invalid configuration of section 'Kafka':\n{msg}");
            });

            var error = Assert.ThrowsException <OptionsValidationException>(() =>
                                                                            sp.GetRequiredService <IOptionsMonitor <AnnotatedOptions> >().CurrentValue);
        }
コード例 #3
0
        public void CustomTest()
        {
            var            services      = new ServiceCollection();
            var            sp            = services.BuildServiceProvider();
            IConfiguration Configuration = OptionExtensions.InitConfiguration();

            services.AddOptions();
            services.ConfigureAndValidate <KafkaOptions>("Kafka", Configuration);

            var error = Assert.ThrowsException <OptionsValidationException>(() =>
                                                                            sp.GetRequiredService <IOptionsMonitor <AnnotatedOptions> >().CurrentValue);
        }