private void Register(ContainerBuilder builder, Type type, bool treatWarningsAsErrors)
        {
            ImmutableArray <object> instances = _keyValueConfiguration.GetInstances(type);

            IValidationObject[] validationObjects = instances.Select(instance =>
                                                                     instance as IValidationObject).Where(item => item != null).ToArray();

            if (validationObjects.Length > 0 && !validationObjects.Any(validatedObject => validatedObject.IsValid))
            {
                _logger.Warning("There are [{ValidationObjectCount}] but no valid instance of type {Type}", validationObjects.Length, type.FullName);

                if (treatWarningsAsErrors)
                {
                    throw new InvalidOperationException($"Could not create instance of type {type.FullName}, the instance is invalid, using configuration chain {(_keyValueConfiguration as MultiSourceKeyValueConfiguration)?.SourceChain}");
                }
            }

            object validInstance = validationObjects.FirstOrDefault(validationObject => validationObject.IsValid);

            object usedInstance = validInstance ?? instances.FirstOrDefault();

            if (usedInstance is null)
            {
                _logger.Error("Could not register URN-mapped type {Type}, instance is null", type);

                return;
            }

            _logger.Debug("Registering URN-bound instance {Instance}, {Type}", usedInstance, usedInstance.GetType().FullName);

            builder
            .RegisterInstance(usedInstance)
            .AsSelf()
            .SingleInstance();
        }
Exemple #2
0
        protected override void Load(ContainerBuilder builder)
        {
            ImmutableArray <MartenConfiguration> configurations = _keyValueConfiguration.GetInstances <MartenConfiguration>();

            if (configurations.IsDefaultOrEmpty)
            {
                return;
            }

            if (configurations.Length > 1)
            {
                throw new Core.DeployerAppException(
                          $"Expected exactly 1 instance of type {nameof(MartenConfiguration)} but got {configurations.Length}");
            }

            MartenConfiguration configuration = configurations.Single();

            if (!string.IsNullOrWhiteSpace(configuration.ConnectionString) && configuration.Enabled)
            {
                builder.RegisterType <MartenStore>()
                .AsImplementedInterfaces()
                .SingleInstance();

                builder.Register(context =>
                                 DocumentStore.For(options => ConfigureMarten(options, configuration.ConnectionString)))
                .AsImplementedInterfaces()
                .AsSelf()
                .SingleInstance();
            }
        }