/// <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 <IVerivoxStartup>(); //create and sort instances of startup configurations IOrderedEnumerable <IVerivoxStartup> instances = startupConfigurations .Select(startup => (IVerivoxStartup)Activator.CreateInstance(startup)) .OrderBy(startup => startup.Order); //configure services foreach (IVerivoxStartup instance in instances) { instance.ConfigureServices(services, configuration); } //register dependencies VerivoxConfig config = services.BuildServiceProvider().GetService <VerivoxConfig>(); _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="VerivoxConfig">Startup Verivox configuration parameters</param> /// <param name="services">Collection of service descriptors</param> /// <param name="typeFinder">Type finder</param> protected virtual IServiceProvider RegisterDependencies(VerivoxConfig dreamlinesConfig, 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); } //populate Autofac container builder with the set of registered service descriptors containerBuilder.Populate(services); //create service provider _serviceProvider = new AutofacServiceProvider(containerBuilder.Build()); return(_serviceProvider); }