/// <summary> /// Add and configure services /// </summary> /// <param name="services">Collection of service descriptors</param> /// <param name="configuration">Configuration of the application</param> /// <returns>Service provider</returns> public IServiceProvider ConfigureServices(IServiceCollection services, IConfiguration configuration) { //find startup configurations provided by other assemblies WebAppTypeFinder typeFinder = new WebAppTypeFinder(); System.Collections.Generic.IEnumerable <Type> startupConfigurations = typeFinder.FindClassesOfType <ICepresStartup>(); //create and sort instances of startup configurations IOrderedEnumerable <ICepresStartup> instances = startupConfigurations .Select(startup => (ICepresStartup)Activator.CreateInstance(startup)) .OrderBy(startup => startup.Order); //configure services foreach (ICepresStartup instance in instances) { instance.ConfigureServices(services, configuration); } //register dependencies CepresConfig config = services.BuildServiceProvider().GetService <CepresConfig>(); _serviceProvider = RegisterDependencies(config, services, typeFinder); //resolve assemblies here. otherwise, plugins can throw an exception when rendering views AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; return(_serviceProvider); }
/// <summary> /// Register dependencies using Autofac /// </summary> /// <param name="FileManagerConfig">Startup FileManager configuration parameters</param> /// <param name="services">Collection of service descriptors</param> /// <param name="typeFinder">Type finder</param> protected virtual IServiceProvider RegisterDependencies(CepresConfig config, IServiceCollection services, ITypeFinder typeFinder) { //var autofacServiceProviderFactory = new AutofacServiceProviderFactory(ConfigureContainer); //var containerBuilder = autofacServiceProviderFactory.CreateBuilder(services); //containerBuilder.Populate(services); //containerBuilder.Build(); //return autofacServiceProviderFactory.CreateServiceProvider(containerBuilder); ContainerBuilder 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 System.Collections.Generic.IEnumerable <Type> dependencyRegistrars = typeFinder.FindClassesOfType <IDependencyRegistrar>(); //create and sort instances of dependency registrars IOrderedEnumerable <IDependencyRegistrar> instances = dependencyRegistrars //.Where(dependencyRegistrar => PluginManager.FindPlugin(dependencyRegistrar)?.Installed ?? true) //ignore not installed plugins .Select(dependencyRegistrar => (IDependencyRegistrar)Activator.CreateInstance(dependencyRegistrar)) .OrderBy(dependencyRegistrar => dependencyRegistrar.Order); //register all provided dependencies foreach (IDependencyRegistrar dependencyRegistrar in instances) { dependencyRegistrar.Register(containerBuilder, config); } //populate Autofac container builder with the set of registered service descriptors containerBuilder.Populate(services); //create service provider _serviceProvider = new AutofacServiceProvider(containerBuilder.Build()); return(_serviceProvider); }