Beispiel #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);
            }
        }
Beispiel #2
0
        public static void AddSettings(this IServiceCollection services)
        {
            var typeFinder = new AppTypeFinder();
            var settings   = typeFinder.FindClassesOfType <ISettings>();
            var instances  = settings.Select(x => (ISettings)Activator.CreateInstance(x));

            foreach (var item in instances)
            {
                services.AddScoped(item.GetType(), (x) =>
                {
                    var type           = item.GetType();
                    var storeId        = string.Empty;
                    var settingService = x.GetRequiredService <ISettingService>();
                    var storeContext   = x.GetRequiredService <IStoreContext>();
                    if (storeContext.CurrentStore == null)
                    {
                        storeId = ""; //storeContext.SetCurrentStore().Result.Id;
                    }
                    else
                    {
                        storeId = storeContext.CurrentStore.Id;
                    }

                    return(settingService.LoadSetting(type, storeId));
                });
            }
        }
Beispiel #3
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);
            }
        }
Beispiel #4
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);
 }
Beispiel #8
0
        /// <summary>
        /// Adds authentication service
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        public static void AddGrandAuthentication(this IServiceCollection services, IConfiguration configuration)
        {
            var config = new GrandConfig();

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

            //set default authentication schemes
            var authenticationBuilder = services.AddAuthentication(options =>
            {
                options.DefaultScheme       = GrandCookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultSignInScheme = GrandCookieAuthenticationDefaults.ExternalAuthenticationScheme;
            });

            //add main cookie authentication
            authenticationBuilder.AddCookie(GrandCookieAuthenticationDefaults.AuthenticationScheme, options =>
            {
                options.Cookie.Name      = config.CookiePrefix + GrandCookieAuthenticationDefaults.AuthenticationScheme;
                options.Cookie.HttpOnly  = true;
                options.LoginPath        = GrandCookieAuthenticationDefaults.LoginPath;
                options.AccessDeniedPath = GrandCookieAuthenticationDefaults.AccessDeniedPath;

                options.Cookie.SecurePolicy = config.CookieSecurePolicyAlways ? CookieSecurePolicy.Always : CookieSecurePolicy.SameAsRequest;
            });

            //add external authentication
            authenticationBuilder.AddCookie(GrandCookieAuthenticationDefaults.ExternalAuthenticationScheme, options =>
            {
                options.Cookie.Name         = config.CookiePrefix + GrandCookieAuthenticationDefaults.ExternalAuthenticationScheme;
                options.Cookie.HttpOnly     = true;
                options.LoginPath           = GrandCookieAuthenticationDefaults.LoginPath;
                options.AccessDeniedPath    = GrandCookieAuthenticationDefaults.AccessDeniedPath;
                options.Cookie.SecurePolicy = config.CookieSecurePolicyAlways ? CookieSecurePolicy.Always : CookieSecurePolicy.SameAsRequest;
            });

            //register external authentication plugins now
            var typeFinder = new AppTypeFinder();
            var externalAuthConfigurations = typeFinder.FindClassesOfType <IExternalAuthenticationRegistrar>();
            //create and sort instances of external authentication configurations
            var externalAuthInstances = externalAuthConfigurations
                                        .Where(x => PluginManager.FindPlugin(x)?.Installed ?? true) //ignore not installed plugins
                                        .Select(x => (IExternalAuthenticationRegistrar)Activator.CreateInstance(x))
                                        .OrderBy(x => x.Order);

            //configure services
            foreach (var instance in externalAuthInstances)
            {
                instance.Configure(authenticationBuilder, configuration);
            }
        }
        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();
        }
Beispiel #10
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);
            }
        }
Beispiel #11
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);
            }
        }
Beispiel #12
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);
        }
Beispiel #13
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));
                    });
                }
            }
        }