public static void RegisterManager(this IServiceCollection services)
        {
            var typeFinder            = new WebAppTypeFinder();
            var managerInterfaceTypes = typeFinder.FindOfType <IManager>(FindType.Interface)
                                        .Where(x => x.Name != nameof(IManager));

            foreach (var managerInterfaceType in managerInterfaceTypes)
            {
                var imp = typeFinder.FindOfType(managerInterfaceType).FirstOrDefault();
                if (imp != null && imp.IsClass)
                {
                    services.AddScoped(managerInterfaceType, imp);
                }
            }
        }
Beispiel #2
0
        /// <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();

            //register engine
            services.AddSingleton <IEngine>(this);

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

            ServiceProvider = services.BuildServiceProvider();

            var startupConfigurations = typeFinder.FindOfType <IAppModuleStartup>();

            //create and sort instances of startup configurations
            var instances = startupConfigurations
                            .Select(startup => (IAppModuleStartup)Activator.CreateInstance(startup))
                            .OrderBy(startup => startup.Order);

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

            //resolve assemblies here. otherwise, plugins can throw an exception when rendering views
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        }
Beispiel #3
0
        /// <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.FindOfType <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);
        }
        public static void AddBindAppModelConfiguation(this IServiceCollection services, IConfiguration configuration,
                                                       AppSettings appSettings)
        {
            configuration.Bind(appSettings);
            appSettings.PreLoadModelConfig();
            //判断是否为新环境,如果是环境执行初始化,判断的条件就是AppData下存不存在AppSettings.json文件
            var isInit = !AppSettingsHelper.ExistAppSettingsFile();

            var typeFinder    = new WebAppTypeFinder();
            var modelSettings = typeFinder.FindOfType <IAppModuleConfig>();
            var instances     = modelSettings
                                .Select(startup => (IAppModuleConfig)Activator.CreateInstance(startup));

            foreach (var appModelConfig in instances)
            {
                var nodeName = appModelConfig?.GetType().Name ?? "TempModel";
                configuration.GetSection($"ModuleConfigurations:{nodeName}").Bind(appModelConfig);

                if (isInit)
                {
                    appModelConfig?.Init();
                }
                appSettings.ModuleConfigurations.Add(nodeName, appModelConfig);
            }

            services.AddSingleton(appSettings);
            AppSettingsHelper.SaveAppSettings(appSettings);
        }
        public static void RegisterIValidatorType(this IServiceCollection services)
        {
            var typeFinder = new WebAppTypeFinder();

            var validatorTypes = typeFinder.FindOfType <IValidator>();

            foreach (var validatorType in validatorTypes)
            {
                var implementedInterface = validatorType.GetInterface("IValidator`1");
                if (implementedInterface != null)
                {
                    services.AddScoped(implementedInterface, validatorType);
                }
            }
            // foreach (var validatorType in types)
            // {
            //     var parentType =
            //         ((System.Reflection.TypeInfo) validatorType).ImplementedInterfaces.FirstOrDefault(x =>
            //             x.Name == "IValidator`1");
            //     if (parentType != null)
            //     {
            //         services.AddScoped(parentType, validatorType);
            //     }
            // }
        }
        public void Configure(IApplicationBuilder application)
        {
            // application.ApplicationServices.GetRequiredService<IShardingBootstrapper>().Start();
            var logger = application.ApplicationServices.GetService(typeof(ILogger <object>)) as ILogger <object>;

            try
            {
                logger.LogInformation("开始执行数据库还原");
                var typeFinder = new WebAppTypeFinder();
                var dbContexts = typeFinder.FindOfType(typeof
                                                       (GirvsDbContext)).Where(x => !x.IsAbstract && !x.IsInterface).ToList();
                if (!dbContexts.Any())
                {
                    return;
                }


                foreach (var dbContext in dbContexts.Select(dbContextType =>
                                                            EngineContext.Current.Resolve(dbContextType) as GirvsDbContext))
                {
                    var dbConfig = EngineContext.Current.GetAppModuleConfig <DbConfig>()
                                   .GetDataConnectionConfig(dbContext.GetType());

                    if (dbConfig is { EnableAutoMigrate: true })
                    {
                        dbContext?.SwitchReadWriteDataBase(DataBaseWriteAndRead.Write);
                        dbContext?.Database.MigrateAsync().Wait();
                    }
                }
        public static void RegisterRepository(this IServiceCollection services)
        {
            var typeFinder = new WebAppTypeFinder();

            //获取所有继承IRepository<>的接口
            var repositoryTypes = typeFinder.FindOfType(typeof(IRepository <,>), FindType.Interface)
                                  .Where(x => !x.Name.Contains("IRepository`"));

            foreach (var repositoryType in repositoryTypes)
            {
                var imp = typeFinder.FindOfType(repositoryType).FirstOrDefault();
                if (imp != null)
                {
                    services.AddScoped(repositoryType, imp);
                }
            }
        }
Beispiel #8
0
        public static void AddCapSubscribe(this IServiceCollection services)
        {
            var typeFinder = new WebAppTypeFinder();
            var subscribes = typeFinder.FindOfType <ICapSubscribe>();

            foreach (var subscribe in subscribes)
            {
                services.AddScoped(subscribe);
            }
        }
        public static void RegisterCommandHandlerType(this IServiceCollection services)
        {
            var typeFinder          = new WebAppTypeFinder();
            var commandHandlerTypes = typeFinder.FindOfType <CommandHandler>()
                                      .Where(x => x.Name != nameof(CommandHandler));

            foreach (var commandHandlerType in commandHandlerTypes)
            {
                foreach (var @interface in commandHandlerType.GetInterfaces())
                {
                    services.AddScoped(@interface, commandHandlerType);
                }
            }
        }
        public static void RegisterNotificationHandlerType(this IServiceCollection services)
        {
            var typeFinder = new WebAppTypeFinder();
            var types      = typeFinder.FindOfType(typeof(INotificationHandler <>));

            foreach (var type in types)
            {
                // var implementedInterface = type.GetInterface("INotificationHandler`1");
                foreach (var implementedInterface in type.GetInterfaces())
                {
                    services.AddScoped(implementedInterface, type);
                }
            }
        }
Beispiel #11
0
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            var typeFinder = new WebAppTypeFinder();

            var refits = typeFinder.FindOfType <IGirvsRefit>(findType: FindType.Interface)
                         .Where(x => x.Name != nameof(IGirvsRefit));

            foreach (var refit in refits)
            {
                if (refit.GetCustomAttribute(typeof(RefitServiceAttribute)) is RefitServiceAttribute refitService)
                {
                    services.AddRefitClient(refit, new RefitSettings(new SystemTextJsonContentSerializer()))
                    // //设置服务名称,andc-api-sys是系统在Consul注册的服务名
                    .ConfigureHttpClient(c => c.BaseAddress = new Uri("http://localhost:5000"))
                    .AddHttpMessageHandler(() => new AuthenticatedHttpClientHandler(refitService));
                }
            }
        }
Beispiel #12
0
        public async Task <dynamic> InitMigration(string verificationCode)
        {
            var result = new List <string>();

            if (verificationCode.ToMd5() == "zhuofan@168".ToMd5())
            {
                _logger.LogInformation("开始执行数据库还原");
                var typeFinder = new WebAppTypeFinder();
                var dbContexts = typeFinder.FindOfType(typeof
                                                       (GirvsDbContext)).Where(x => !x.IsAbstract && !x.IsInterface).ToList();

                if (!dbContexts.Any())
                {
                    throw new GirvsException("not found dbcontext");
                }


                foreach (var dbContext in dbContexts.Select(dbContextType =>
                                                            EngineContext.Current.Resolve(dbContextType) as GirvsDbContext))
                {
                    var dbConfig = EngineContext.Current.GetAppModuleConfig <DbConfig>()
                                   .GetDataConnectionConfig(dbContext.GetType());
                    try
                    {
                        dbContext.SwitchReadWriteDataBase(DataBaseWriteAndRead.Write);
                        await dbContext?.Database.MigrateAsync();

                        result.Add($"数据库{dbConfig.Name}:迁移成功!");
                    }
                    catch (Exception e)
                    {
                        result.Add($"数据库{dbConfig.Name}:迁移失败!");
                        _logger.LogError(e, $"数据库{dbConfig.Name}:迁移失败!");
                    }
                }
            }
            else
            {
                result.Add($"较验码输入不正确,请重新输入!");
            }

            return(result);
        }
Beispiel #13
0
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            var typeFinder           = new WebAppTypeFinder();
            var mapperConfigurations = typeFinder.FindOfType <IOrderedMapperProfile>();

            var instances = mapperConfigurations
                            .Select(mapperConfiguration => (IOrderedMapperProfile)Activator.CreateInstance(mapperConfiguration))
                            .OrderBy(mapperConfiguration => mapperConfiguration.Order);

            var config = new MapperConfiguration(cfg =>
            {
                //cfg.AddProfile<DefaultProfile>();
                foreach (var instance in instances)
                {
                    cfg.AddProfile(instance.GetType());
                }
            });

            services.AddSingleton(typeof(IMapper), config.CreateMapper());
        }
        /// <summary>
        /// 注册数据库基础对象上下文
        /// </summary>
        public static void AddGirvsObjectContext(this IServiceCollection services)
        {
            var typeFinder = new WebAppTypeFinder();
            var dbContexts = typeFinder.FindOfType(typeof
                                                   (GirvsDbContext)).Where(x => !x.IsAbstract && !x.IsInterface).ToList();

            if (!dbContexts.Any())
            {
                return;
            }
            var serviceType = typeof(DataProviderServiceExtensions);
            var mi          = serviceType.GetMethod(nameof(AddGirvsDbContext));

            if (mi == null)
            {
                return;
            }
            foreach (var dbContext in dbContexts)
            {
                var dmi    = mi.MakeGenericMethod(dbContext);
                var config = EngineContext.Current.GetAppModuleConfig <DbConfig>()?.GetDataConnectionConfig(dbContext);
                dmi.Invoke(serviceType, new object[] { services, config });
            }
        }