Beispiel #1
0
        /// <summary>
        /// Register dependencies using Autofac
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        /// <param name="typeFinder">Type finder</param>
        protected virtual IServiceProvider RegisterDependencies(IServiceCollection services, ITypeFinder typeFinder)
        {
            var containerBuilder = new ContainerBuilder();

            //register engine
            containerBuilder.RegisterInstance(this).As <IEngine>().SingleInstance();

            //register type finder
            containerBuilder.RegisterInstance(typeFinder).As <ITypeFinder>().SingleInstance();

            //find dependency registrars provided by other assemblies
            var dependencyRegistrars = typeFinder.FindClassesOfType <IDependencyRegistrar>();

            //create and sort instances of dependency registrars
            var instances = dependencyRegistrars
                            //.Where(dependencyRegistrar => PluginManager.FindPlugin(dependencyRegistrar).Return(plugin => plugin.Installed, true)) //ignore not installed plugins
                            .Select(dependencyRegistrar => (IDependencyRegistrar)Activator.CreateInstance(dependencyRegistrar))
                            .OrderBy(dependencyRegistrar => dependencyRegistrar.Order);

            //register all provided dependencies
            foreach (var dependencyRegistrar in instances)
            {
                dependencyRegistrar.Register(containerBuilder, typeFinder);
            }

            //populate Autofac container builder with the set of registered service descriptors
            containerBuilder.Populate(services);

            //create service provider
            var container = containerBuilder.Build();

            ServiceProvider  = new AutofacServiceProvider(container);
            containerManager = new AutofacContainerManager(container);
            return(ServiceProvider);
        }
Beispiel #2
0
        private void InitializeContainer(IServiceCollection services, ContainerConfigurer configurer)
        {
            var builder = new ContainerBuilder();

            if (!services.IsNullOrEmpty())
            {
                builder.Populate(services);
            }

            var container = builder.Build();

            containerManager = new AutofacContainerManager(container);
            configurer.Configure(this, containerManager);

            //_containerManager.NotifyCompleted(container);
        }