Example #1
0
        public void ConfigureServices(IServiceCollection services, IConfigurationRoot configuration)
        {
            var typeFinder            = new AppTypeFinder(new AppAssemblyProvider());
            var startupConfigurations = typeFinder.FindClassesOfType <IAppStartup>();

            var instances = startupConfigurations
                            .Select(startup => (IAppStartup)Activator.CreateInstance(startup))
                            .OrderBy(startup => startup.Order);

            foreach (var instance in instances)
            {
                instance.ConfigureServices(services, configuration);
            }

            AddAutoMapper(services, typeFinder);

            var appConfig = services.BuildServiceProvider().GetService <AppConfig>();

            RegisterDependencies(appConfig, services, typeFinder);

            if (!appConfig.IgnoreStartupTasks)
            {
                RunStartupTasks(typeFinder);
            }
        }
Example #2
0
        /// <summary>
        /// Add and configure services
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        /// <param name="configuration">Configuration root of the application</param>
        /// <returns>Service provider</returns>
        public static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            //find startup configurations provided by other assemblies
            var typeFinder            = new AppTypeFinder();
            var startupConfigurations = typeFinder.FindClassesOfType <IGrandStartup>();

            //create and sort instances of startup configurations
            var instances = startupConfigurations
                            //.Where(startup => PluginManager.FindPlugin(startup).Return(plugin => plugin.Installed, true)) //ignore not installed plugins
                            .Select(startup => (IGrandStartup)Activator.CreateInstance(startup))
                            .OrderBy(startup => startup.Order);

            //configure services
            foreach (var instance in instances)
            {
                instance.ConfigureServices(services, configuration);
            }

            //register mapper configurations
            AddAutoMapper(typeFinder);

            //Register custom type converters
            AddTypeConverter(typeFinder);

            var config = new GrandConfig();

            configuration.GetSection("Grand").Bind(config);

            //run startup tasks
            if (!config.IgnoreStartupTasks)
            {
                RunStartupTasks(typeFinder);
            }
        }
Example #3
0
        /// <summary>
        /// ConfigureContainer is where you can register things directly
        /// with Autofac. This runs after ConfigureServices so the things
        /// here will override registrations made in ConfigureServices.
        /// </summary>
        /// <param name="serviceCollection">Service Collection</param>
        /// <param name="configuration">Configuration</param>
        public static void ConfigureContainer(IServiceCollection serviceCollection, IConfiguration configuration)
        {
            var typeFinder = new AppTypeFinder();

            //register type finder
            serviceCollection.AddSingleton <ITypeFinder>(typeFinder);

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

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

            var config = new GrandConfig();

            configuration.GetSection("Grand").Bind(config);

            //register all provided dependencies
            foreach (var dependencyRegistrar in instances)
            {
                dependencyRegistrar.Register(serviceCollection, typeFinder, config);
            }
        }
        public void TestAppTypeFinder()
        {
            AppTypeFinder typeFinder = new AppTypeFinder(new List <IAssemblyLoader>()
            {
                new DependencyContextLoader()
            });
            var types = typeFinder.FindClassesOfType <IAssemblyLoader>();

            Assert.True(types.Where(type => type == typeof(DependencyContextLoader)).Count() > 0);
        }
 public void TestClassFindConfigPattern()
 {
     ITypeFinder typeFinder = new AppTypeFinder(new IAssemblyLoader[] { new AppDomainLoader() });
     var types = typeFinder.FindClassesOfType<UnitTestAppTypeFinder>();
     int count = 0;
     foreach (var type in types)
     {
         Assert.AreEqual(type.FullName, "TecPrime.AssemblyClassFinder.Test.UnitTestAppTypeFinder");
         count++;
     }
     Assert.AreEqual(count, 1);
 }
 public void TestClassFindSkipeFail()
 {
     ITypeFinder typeFinder = new AppTypeFinder(new IAssemblyLoader[] { new AppDomainLoader() });
     typeFinder.SkipPattern = "^TecPrime";
     typeFinder.RestrictToPattern = "";
     var types = typeFinder.FindClassesOfType<UnitTestAppTypeFinder>();
     int count = 0;
     foreach (var type in types)
     {
         count++;
     }
     Assert.AreEqual(count, 0);
 }
        public static IContainer RegisterDependencies(IList<IAssemblyLoader> loaders)
        {
            ContainerBuilder builder = new ContainerBuilder();
            AppTypeFinder typeFinder = new AppTypeFinder(loaders);
            var drTypes = typeFinder.FindClassesOfType<IRegisterDependency>();
            var drInstances = new List<IRegisterDependency>();
            foreach (var drType in drTypes)
                drInstances.Add((IRegisterDependency)Activator.CreateInstance(drType));

            drInstances = drInstances.AsQueryable().OrderBy(t => t.Order).ToList();
            foreach (var dependencyRegistrar in drInstances)
                dependencyRegistrar.Register(builder);

            return builder.Build();
        }
Example #8
0
        private void RegisterDependencies(ODataConventionModelBuilder builder, ApiConfig apiConfig)
        {
            var typeFinder = new AppTypeFinder();

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

            //create and sort instances of dependency registrars
            var instances = dependencyRegistrars
                            .Select(dependencyRegistrar => (IDependencyEdmModel)Activator.CreateInstance(dependencyRegistrar))
                            .OrderBy(dependencyRegistrar => dependencyRegistrar.Order);

            //register all provided dependencies
            foreach (var dependencyRegistrar in instances)
            {
                dependencyRegistrar.Register(builder, apiConfig);
            }
        }
Example #9
0
        /// <summary>
        /// Configure HTTP request pipeline
        /// </summary>
        /// <param name="application">Builder for configuring an application's request pipeline</param>
        /// <param name="webHostEnvironment">WebHostEnvironment</param>
        public static void ConfigureRequestPipeline(IApplicationBuilder application, IWebHostEnvironment webHostEnvironment)
        {
            //find startup configurations provided by other assemblies
            var typeFinder            = new AppTypeFinder();
            var startupConfigurations = typeFinder.FindClassesOfType <IGrandStartup>();

            //create and sort instances of startup configurations
            var instances = startupConfigurations
                            //.Where(startup => PluginManager.FindPlugin(startup).Return(plugin => plugin.Installed, true)) //ignore not installed plugins
                            .Select(startup => (IGrandStartup)Activator.CreateInstance(startup))
                            .OrderBy(startup => startup.Order);

            //configure request pipeline
            foreach (var instance in instances)
            {
                instance.Configure(application, webHostEnvironment);
            }
        }
Example #10
0
        public static IServiceCollection RegisterDependencies(IList <IAssemblyLoader> loaders)
        {
            IServiceCollection collection = new ServiceCollection();
            AppTypeFinder      typeFinder = new AppTypeFinder(loaders);
            var drTypes     = typeFinder.FindClassesOfType <IRegisterDependency>();
            var drInstances = new List <IRegisterDependency>();

            foreach (var drType in drTypes)
            {
                drInstances.Add((IRegisterDependency)Activator.CreateInstance(drType));
            }

            drInstances = drInstances.AsQueryable().OrderBy(t => t.Order).ToList();
            foreach (var dependencyRegistrar in drInstances)
            {
                dependencyRegistrar.Register(collection);
            }

            return(collection);
        }
Example #11
0
        /// <summary>
        /// Add and configure any of the middleware
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        /// <param name="configuration">Configuration root of the application</param>
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            //database is already installed, so start scheduled tasks
            if (DataSettingsHelper.DatabaseIsInstalled())
            {
                var typeFinder    = new AppTypeFinder();
                var scheduleTasks = typeFinder.FindClassesOfType <IScheduleTask>();

                var scheduleTasksInstalled = scheduleTasks
                                             .Where(t => PluginManager.FindPlugin(t).Return(plugin => plugin.Installed, true)); //ignore not installed plugins

                foreach (var task in scheduleTasksInstalled)
                {
                    var assemblyName = task.Assembly.GetName().Name;
                    services.AddSingleton <IHostedService, BackgroundServiceTask>(sp =>
                    {
                        return(new BackgroundServiceTask($"{task.FullName}, {assemblyName}", sp));
                    });
                }
            }
        }
Example #12
0
        public override IList <Setting> GetAllSettings()
        {
            string directory             = new AppTypeFinder().GetBinDirectory();
            var    configurationBasePath = "";

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                configurationBasePath = directory.Substring(0, directory.IndexOf("\\Tests\\Grand.Services.Tests\\") + 27);
            }
            else
            {
                configurationBasePath = directory.Substring(0, directory.IndexOf("/Tests/Grand.Services.Tests/") + 27);
            }

            var configuration = new ConfigurationBuilder()
                                .SetBasePath(configurationBasePath)
                                .AddJsonFile("appsettingstest.json", optional: false, reloadOnChange: true)
                                .Build();

            var settings      = new List <Setting>();
            var settingObject = new ServiceCollection().ConfigureStartupConfig <ApplicationSettings>(configuration.GetSection("ApplicationSettingsSection"));
            var properties    = settingObject.GetType().GetProperties();

            foreach (var property in properties)
            {
                var value = settingObject.GetType().GetProperty(property.Name).GetValue(settingObject, null);
                settings.Add(new Setting
                {
                    Name    = property.Name.ToLowerInvariant(),
                    Value   = value.ToString(),
                    StoreId = ""
                });
            }

            return(settings);
        }