Exemple #1
0
        /// <summary>
        /// Registers <see cref="JsonFileConfigurationLoader"/> and <see cref="JsonFileConfigurationProvider"/> with a specific registrationKey.
        /// </summary>
        /// <param name="builder">Autofac container builder.</param>
        /// <param name="configurationFilePaths">Paths to the configuration files. The first file is considred to be the main file, the others are overrides.</param>
        /// <exception cref="ArgumentNullException">Is thrown if the <paramref name="builder"/> or the <paramref name="configurationFilePaths"/> is null.</exception>
        /// <exception cref="ArgumentException">Is thrown if the <paramref name="configurationFilePaths"/> is an empty string.</exception>
        /// <returns>A registrationKey the <see cref="IConfigurationProvider{TRawDataIn,TRawDataOut}"/> is registered with.</returns>
        public static AutofacConfigurationProviderKey RegisterKeyedJsonFileConfigurationProvider(this ContainerBuilder builder, params string[] configurationFilePaths)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (configurationFilePaths == null)
            {
                throw new ArgumentNullException(nameof(configurationFilePaths));
            }
            if (configurationFilePaths.Any(path => String.IsNullOrWhiteSpace(path)))
            {
                throw new ArgumentException("At least one of the configuration file path is empty.", nameof(configurationFilePaths));
            }

            var key = new AutofacConfigurationProviderKey();

            RegisterConverterOnce(builder);
            builder.Register(context => new JsonFileConfigurationLoader(context.Resolve <IFile>(), context.Resolve <IJsonTokenConverter>(), configurationFilePaths))
            .Keyed <IConfigurationLoader <JToken, JToken> >(key)
            .SingleInstance();
            builder.Register(context => context.ResolveKeyed <IConfigurationLoader <JToken, JToken> >(key).Load())
            .Keyed <IConfigurationProvider <JToken, JToken> >(key)
            .SingleInstance();

            return(key);
        }
        public static AutofacConfigurationProviderKey RegisterKeyedMicrosoftConfigurationProvider([NotNull] this ContainerBuilder builder, [NotNull] IConfiguration configuration, [CanBeNull] CultureInfo converterCulture = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            builder.RegisterDefaultMicrosoftConfigurationTypes(converterCulture);

            var key = new AutofacConfigurationProviderKey();

            builder.RegisterInstance(new MicrosoftConfigurationChangeTokenSource(configuration))
            .Keyed <MicrosoftConfigurationChangeTokenSource>(key);

            builder.RegisterType <MicrosoftConfigurationLoader>()
            .WithParameter(new TypedParameter(typeof(IConfiguration), configuration))
            .Keyed <IConfigurationLoader <IConfiguration, IConfiguration> >(key)
            .SingleInstance();

            builder.Register(context => context.ResolveKeyed <IConfigurationLoader <IConfiguration, IConfiguration> >(key).Load())
            .Keyed <IConfigurationProvider <IConfiguration, IConfiguration> >(key)
            .SingleInstance();

            return(key);
        }
Exemple #3
0
        /// <summary>
        /// Registeres a type that will be used with <see cref="IConfigurationProvider{TRawDataIn,TRawDataOut}.GetConfiguration{T}"/>.
        /// </summary>
        /// <typeparam name="T">Type of the configuration</typeparam>
        /// <param name="builder">Container builder.</param>
        /// <param name="registrationKey">The key of a <see cref="IConfigurationProvider{TRawDataIn,TRawDataOut}"/>.</param>
        /// <param name="propertyName">Tne name of the json property to use to build the configuration.</param>
        /// <exception cref="ArgumentNullException">Is thrown if the <paramref name="builder"/> or the <paramref name="registrationKey"/> is null.</exception>
        /// <returns>Autofac registration builder.</returns>
        public static IRegistrationBuilder <T, SimpleActivatorData, SingleRegistrationStyle> RegisterJsonFileConfiguration <T>(this ContainerBuilder builder, AutofacConfigurationProviderKey registrationKey, string propertyName = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (registrationKey == null)
            {
                throw new ArgumentNullException(nameof(registrationKey));
            }

            var selector = String.IsNullOrWhiteSpace(propertyName) ? null : new JsonFileConfigurationSelector(propertyName.Trim());

            RegisterTypeOnce <T>(builder);

            return(builder.Register(context => context.ResolveKeyed <IConfigurationProvider <JToken, JToken> >(registrationKey).GetConfiguration <T>(selector)));
        }
        /// <summary>
        /// Registeres a type that will be used with <see cref="IConfigurationProvider{TRawDataIn,TRawDataOut}.GetConfiguration{T}"/>.
        /// </summary>
        /// <typeparam name="T">Type of the configuration</typeparam>
        /// <param name="builder">Container builder.</param>
        /// <param name="registrationKey">The key of a <see cref="IConfigurationProvider{TRawDataIn,TRawDataOut}"/>.</param>
        /// <param name="key">The key of the configuration section.</param>
        /// <param name="reuseInstance">If <c>true</c> then the configuration of type <typeparamref name="T"/> will be reused until the unterlying <see cref="IConfiguration"/> has been changed.</param>
        /// <exception cref="ArgumentNullException">Is thrown if the <paramref name="builder"/> or the <paramref name="registrationKey"/> is null.</exception>
        /// <returns>Autofac registration builder.</returns>
        public static IRegistrationBuilder <T, SimpleActivatorData, SingleRegistrationStyle> RegisterMicrosoftConfiguration <T>([NotNull] this ContainerBuilder builder, [NotNull] AutofacConfigurationProviderKey registrationKey, [CanBeNull] string key = null, bool reuseInstance = true)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (registrationKey == null)
            {
                throw new ArgumentNullException(nameof(registrationKey));
            }

            // ReSharper disable once PossibleNullReferenceException
            var selector = String.IsNullOrWhiteSpace(key) ? null : new MicrosoftConfigurationSelector(key.Trim());

            builder.TryRegisterTypeOnce <T>();

            if (reuseInstance)
            {
                builder.RegisterType <MicrosoftConfigurationCache <T> >()
                .WithParameter(new ResolvedParameter((info, context) => info.ParameterType == typeof(IConfigurationProvider <IConfiguration, IConfiguration>), (info, context) => context.ResolveKeyed <IConfigurationProvider <IConfiguration, IConfiguration> >(registrationKey)))
                .WithParameter(new TypedParameter(typeof(IConfigurationSelector <IConfiguration, IConfiguration>), selector))
                .WithParameter(new ResolvedParameter((info, context) => info.ParameterType == typeof(MicrosoftConfigurationChangeTokenSource), (info, context) => context.ResolveKeyed <MicrosoftConfigurationChangeTokenSource>(registrationKey)))
                .Keyed <IConfigurationCache <T> >(registrationKey)
                .SingleInstance();

                return(builder.Register(context => context.ResolveKeyed <IConfigurationCache <T> >(registrationKey).CurrentValue));
            }

            return(builder.Register(context => context.ResolveKeyed <IConfigurationProvider <IConfiguration, IConfiguration> >(registrationKey).GetConfiguration <T>(selector)));
        }
Exemple #5
0
        public static IRegistrationBuilder <T, SimpleActivatorData, SingleRegistrationStyle> RegisterJsonFileConfiguration <T>([NotNull] this ContainerBuilder builder, [NotNull] AutofacConfigurationProviderKey registrationKey, [CanBeNull] string propertyName = null, bool resolveNewInstanceIfNull = true)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (registrationKey == null)
            {
                throw new ArgumentNullException(nameof(registrationKey));
            }

            var selector = String.IsNullOrWhiteSpace(propertyName) ? null : new JsonFileConfigurationSelector(propertyName.Trim());

            RegisterTypeOnce <T>(builder);

            return(builder.Register(context =>
            {
                var config = context.ResolveKeyed <IConfigurationProvider <JToken, JToken> >(registrationKey).GetConfiguration <T>(selector);
                return ReferenceEquals(config, null) && resolveNewInstanceIfNull ? context.ResolveConfigurationType <T>() : config;
            }));
        }