コード例 #1
0
        /// <summary>
        /// Validates the provider config section.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="configuration">The configuration.</param>
        /// <param name="typeLoader">The type loader.</param>
        /// <returns>true if the validation succeeded or false if it doesn't and will log a warning</returns>
        protected bool ValidateProviderConfigSection <T>(AbstractProviderConfigurationSection configuration, TypeLoaderElement typeLoader)
        {
            // ElementInformation.Source appears to be one of few ways to determine if the config-section exists or was returned on-demand with default values
            if (configuration != null && !string.IsNullOrEmpty(configuration.ElementInformation.Source))
            {
                return(true);
            }
            else
            {
                LogHelper.Warn <T>("Cannot register dependencies for provider {0} because ProviderConfigurationSection was specified with key '{1}' but no matching configSection was found. The provider may not be installed correctly.",
                                   ProviderKey,
                                   typeLoader.ConfigSectionKey);

                return(false);
            }
        }
コード例 #2
0
        internal static void Run(IContainerBuilder containerBuilder, string providerKey, Type type, TypeLoaderElement element)
        {
            LogHelper.TraceIfEnabled <ProviderDemandRunner>("Calling setup module for {0} in {1}", () => providerKey, () => type.Assembly.GetName().Name);

            // Check to see if the type has ProviderDemandsDependenciesAttribute, and if so, invoke the builder
            var attribs        = type.GetCustomAttributes(typeof(DemandsDependenciesAttribute), true).OfType <DemandsDependenciesAttribute>();
            var demandBuilders = attribs.Select(attrib => Activator.CreateInstance(attrib.DemandBuilderType)).OfType <AbstractProviderDependencyBuilder>().ToList();

            if (!demandBuilders.Any())
            {
                // The provider does not implement a demand builder, in which case we need to at least scaffold a noop bootstrapper so the installer can cope
                // and also a noop dependency helper
                LogHelper.TraceIfEnabled <ProviderDemandRunner>("No demand builders found of type AbstractProviderDependencyBuilder for {0} in {1}", () => providerKey, () => type.Assembly.GetName().Name);
                RegisterNoopBootstrapper(containerBuilder, providerKey);
                RegisterNoopDependencyHelper(containerBuilder, providerKey);
            }
            else
            {
                foreach (var demandBuilder in demandBuilders)
                {
                    demandBuilder.ProviderKey = providerKey;

                    demandBuilder.RegistryConfigElement = element;

                    // First, run Initialise to give the demand builder a chance to set things up
                    demandBuilder.Initialise(containerBuilder.Context);

                    // If the builder can build, run the rest
                    if (demandBuilder.CanBuild)
                    {
                        containerBuilder.AddDependencyDemandBuilder(demandBuilder);
                    }

                    // Now get the factory for the provider's bootstrapper and register that
                    var factory = demandBuilder.GetProviderBootstrapperFactory(containerBuilder.Context);
                    if (factory == null)
                    {
                        RegisterNoopBootstrapper(containerBuilder, providerKey);
                    }
                    else
                    {
                        LogHelper.TraceIfEnabled <ProviderDemandRunner>("Registering a bootstrapper for {0}", () => providerKey);
                        containerBuilder.ForFactory(factory)
                        .Named <AbstractProviderBootstrapper>(providerKey)
                        .ScopedAs.Singleton();
                    }

                    // Now get the factory for the provider's dependency helper and register that
                    var helperFactory = demandBuilder.GetProviderDependencyHelperFactory(containerBuilder.Context);
                    if (helperFactory == null)
                    {
                        RegisterNoopDependencyHelper(containerBuilder, providerKey);
                    }
                    else
                    {
                        LogHelper.TraceIfEnabled <ProviderDemandRunner>("Registering a ProviderDependencyHelper for {0}", () => providerKey);
                        containerBuilder.ForFactory(helperFactory)
                        .NamedForSelf(providerKey)
                        .ScopedAs.Singleton();
                    }
                }
            }
        }