private void WatcherOnChanged(object sender, FileSystemEventArgs fileSystemEventArgs) { ImmutableArray <Type> types = _configurationHolder.RegisteredTypes; var jsonKeyValueConfiguration = new JsonKeyValueConfiguration(_fileName); foreach (Type type in types) { ImmutableArray <INamedInstance <object> > allInstances = jsonKeyValueConfiguration.GetNamedInstances(type); foreach (INamedInstance <object> instance in allInstances) { if (instance.Value is IValidationObject validationObject) { if (validationObject.IsValid) { _configurationHolder.Add(instance); } } } } }
private void Register(ContainerBuilder builder, Type type, bool treatWarningsAsErrors) { ImmutableArray <INamedInstance <object> > instances = _keyValueConfiguration.GetNamedInstances(type); if (instances.IsDefaultOrEmpty) { var optionalAttribute = type.GetCustomAttribute <OptionalAttribute>(); if (optionalAttribute != null) { return; } if (treatWarningsAsErrors) { throw new Core.DeployerAppException($"Could not get any instance of type {type.FullName}"); } _logger.Warning("Could not get any instance of type {Type}", type); return; } ImmutableArray <INamedInstance <IValidationObject> > validationObjects = instances .OfType <INamedInstance <IValidationObject> >() .Where(item => item != null) .ToImmutableArray(); if (!validationObjects.IsDefaultOrEmpty && validationObjects.Length > 0 && !validationObjects.Any(validatedObject => validatedObject.Value.IsValid)) { _logger.Warning("There are [{ValidationObjectCount}] but no valid instance of type {Type}", validationObjects.Length, type.FullName); if (treatWarningsAsErrors) { throw new Core.DeployerAppException( $"Could not create instance of type {type.FullName}, the instance is invalid, using configuration chain {(_keyValueConfiguration as MultiSourceKeyValueConfiguration)?.SourceChain}"); } } ImmutableArray <INamedInstance <IValidationObject> > validInstances = validationObjects .Where(validationObject => validationObject.Value.IsValid) .ToImmutableArray(); if (!validInstances.IsDefaultOrEmpty && validInstances.Length == 1) { INamedInstance <IValidationObject> validationObject = validInstances.Single(); _logger.Debug("Registering URN-bound instance {Instance}, {Type}", validationObject, validationObject.GetType().FullName); _configurationHolder.Add(validationObject); builder .Register(context => _configurationHolder.Get(validationObject.Value.GetType(), validationObject.Name)) .As(validationObject.Value.GetType()); } else if (!validInstances.IsDefaultOrEmpty && validInstances.Length > 1) { foreach (INamedInstance <IValidationObject> validationObject in validInstances) { _logger.Debug("Registering URN-bound instance {Instance}, {Type}", validationObject, validationObject.GetType().FullName); _configurationHolder.Add(validationObject); builder .Register(context => _configurationHolder.Get(validationObject.Value.GetType(), validationObject.Name)) .As(validationObject.Value.GetType()); } } else if (!validationObjects.IsDefaultOrEmpty) { foreach (INamedInstance <IValidationObject> validationObject in validationObjects) { _logger.Debug("Registering invalid URN-bound instance {Instance}, {Type}", validationObject, validationObject.GetType().FullName); _configurationHolder.Add(validationObject); builder .Register(context => _configurationHolder.Get(validationObject.Value.GetType(), validationObject.Name)) .As(validationObject.Value.GetType()); } } else { foreach (INamedInstance <object> instance in instances) { _logger.Debug("Registering URN-bound instance {Instance}, {Type}", instance, instance.GetType().FullName); _configurationHolder.Add(instance); builder .Register(context => _configurationHolder.Get(instance.Value.GetType(), instance.Name)) .As(instance.Value.GetType()); } } }