Esempio n. 1
0
        /// <summary>
        /// 通过Autofac注册依赖
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>

        protected virtual IServiceProvider RegisterDependencies(IServiceCollection services, ITypeFinder typeFinder)
        {
            var containerBuilder = new ContainerBuilder();

            containerBuilder.RegisterInstance(this).As <ILCoreEngine>().SingleInstance();

            containerBuilder.RegisterInstance(typeFinder).As <ITypeFinder>().SingleInstance();

            var registrars = typeFinder.FindTypesByInterface <IDependencyRegistrar>();

            var instances = registrars
                            .Select(r => (IDependencyRegistrar)Activator.CreateInstance(r))
                            .OrderBy(r => r.Order);

            foreach (var register in instances)
            {
                register.Register(containerBuilder);
            }

            containerBuilder.Populate(services);
            var container = containerBuilder.Build();

            ServiceProvider = new AutofacServiceProvider(container);
            //
            ContainerManager.SetContainer(container);
            return(ServiceProvider);
        }