public static IServiceCollection AddDistributedRedisCache(this IServiceCollection services)
        {
            // 读取缓存配置文件
            var cacheConfig = ConfigContext.GetJsonConfig <CacheConfig>(opts =>
            {
                opts.Optional   = true;
                opts.ConfigFile = "cache.config.json";
            });

            // 添加分布式缓存配置文件
            if (cacheConfig.EnabledDistributedCache)
            {
                services.AddSingleton <IDistributedCache>(sp =>
                {
                    // https://github.com/2881099/Microsoft.Extensions.Caching.CSRedis
                    CSRedisClient csClient;
                    if (!cacheConfig.EnabledCluster)
                    {
                        csClient = new CSRedisClient(cacheConfig.ConnectionString);
                    }
                    else
                    {
                        csClient = new CSRedisClient(connectionString: null, sentinels: cacheConfig.ConnectionStrings);
                    }
                    return(new CSRedisCache(csClient));
                });
            }
            else
            {
                services.AddDistributedMemoryCache();
            }

            return(services);
        }
Example #2
0
        public override void ConfigureServices(IServiceCollection services)
        {
            // regist
            services.AddSingleton <IWeChatContainer, WeChatContainer>();

            // regist services.
            services.AddSingleton <IAppService, AppService>();
            services.AddSingleton <IMediaService, MediaService>();
            services.AddSingleton <IMenuService, MenuService>();
            services.AddSingleton <INewsService, NewsService>();
            services.AddSingleton <IActivityService, ActivityService>();
            services.AddSingleton <IVoteService, VoteService>();

            // wechat config.
            var configuration = ConfigContext.GetJsonConfig(opts =>
            {
                opts.Optional   = true;
                opts.ConfigFile = "wechat.config.json";
            });

            // Senparc.CO2NET
            services.AddSenparcGlobalServices(configuration);

            // Senparc.Weixin
            services.AddSenparcWeixinServices(configuration);
        }
Example #3
0
        public void ConfigWithNotBuild()
        {
            var testConfig = ConfigContext.GetJsonConfig <TestConfig>(opts =>
            {
                opts.Name       = "test";
                opts.ConfigFile = "test.json";
            });

            Assert.AreNotEqual(testConfig, null);
            Assert.AreEqual(testConfig.SubConfig.SubName, "world");
            Assert.AreEqual(testConfig.SubConfig.List[4], 4);
        }
Example #4
0
        public void Initialize()
        {
            var root = Path.Combine(AppContext.BaseDirectory, "Modules");

            logger.LogDebug($"Start loading modules from {root}.");

            foreach (var moduleFolder in Directory.GetDirectories(root))
            {
                var module = ConfigContext.GetJsonConfig <ModuleConfig>(opts =>
                {
                    opts.ConfigFile = "module.config.json";
                    opts.ConfigPath = moduleFolder;
                });

                Type startupType = null;
                if (module.StartupType.HasValue())
                {
                    // auto load startup type directly.
                    startupType = Type.GetType(module.StartupType);
                }
                if (startupType == null)
                {
                    var assembly = Assembly.LoadFrom(Path.Combine(moduleFolder, module.MainAssembly));
                    startupType = assembly.GetTypes().FirstOrDefault(t => typeof(IStartup).IsAssignableFrom(t));

                    // load mvc ApplicationPart
                    var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
                    foreach (var part in partFactory.GetApplicationParts(assembly))
                    {
                        logger?.LogDebug($"Found mvc application part: {part.Name}.");
                        mvcBuilder.PartManager.ApplicationParts.Add(part);
                    }
                }

                // this simple add/replace module.
                var startup = (IStartup)Dynamic.GetInstanceCreator(startupType)();
                Modules[module.Name] = new ModuleWarpper(module, startup);
                logger.LogDebug($"Found module {module.Name} with type: {startupType.FullName}.");
            }
        }
Example #5
0
        private void LoadWidgets(string parentPath, DirectoryInfo[] dirs)
        {
            foreach (var subDir in dirs)
            {
                var path         = $"{parentPath}/{subDir.Name}";
                var widgetConfig = ConfigContext.GetJsonConfig <WidgetConfig>(opts =>
                {
                    opts.Optional   = true;
                    opts.ConfigPath = path;
                    opts.ConfigFile = "widget.config.json";
                });

                if (widgetConfig.Name.HasValue())
                {
                    widgetService.Regist(path, parentPath.Split('/').Last(), widgetConfig);
                }
                else
                {
                    LoadWidgets(path, subDir.GetDirectories());
                }
            }
        }