/// <summary> /// Register dependencies /// </summary> /// <param name="services">Collection of service descriptors</param> /// <param name="appSettings">App settings</param> public virtual void RegisterDependencies(IServiceCollection services, AppSettings appSettings) { var typeFinder = new WebAppTypeFinder(); //register engine services.AddSingleton <IEngine>(this); //register type finder services.AddSingleton <ITypeFinder>(typeFinder); //find dependency registrars provided by other assemblies var dependencyRegistrars = typeFinder.FindClassesOfType <IDependencyRegistrar>(); //create and sort instances of dependency registrars var instances = dependencyRegistrars .Select(dependencyRegistrar => (IDependencyRegistrar)Activator.CreateInstance(dependencyRegistrar)) .OrderBy(dependencyRegistrar => dependencyRegistrar.Order); //register all provided dependencies foreach (var dependencyRegistrar in instances) { dependencyRegistrar.Register(services, typeFinder, appSettings); } services.AddSingleton(services); }
/// <summary> /// Add and configure services /// </summary> /// <param name="services">Collection of service descriptors</param> /// <param name="configuration">Configuration of the application</param> public void ConfigureServices(IServiceCollection services, IConfiguration configuration) { //find startup configurations provided by other assemblies var typeFinder = new WebAppTypeFinder(); var startupConfigurations = typeFinder.FindClassesOfType <INopStartup>(); //create and sort instances of startup configurations var instances = startupConfigurations .Select(startup => (INopStartup)Activator.CreateInstance(startup)) .OrderBy(startup => startup.Order); //configure services foreach (var instance in instances) { instance.ConfigureServices(services, configuration); } //register mapper configurations AddAutoMapper(services, typeFinder); //run startup tasks RunStartupTasks(typeFinder); //resolve assemblies here. otherwise, plugins can throw an exception when rendering views AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; }