public static CapOptions UseCapOptions(this CapOptions @this, IConfiguration Configuration)
        {
            IConfigurationSection defaultStorage      = Configuration.GetSection("CAP:DefaultStorage");
            IConfigurationSection defaultMessageQueue = Configuration.GetSection("CAP:DefaultMessageQueue");

            if (Enum.TryParse(defaultStorage.Value, out CapStorageType capStorageType))
            {
                if (!Enum.IsDefined(typeof(CapStorageType), capStorageType))
                {
                    Log.Error($"CAP配置CAP:DefaultStorage:{defaultStorage.Value}无效");
                }

                switch (capStorageType)
                {
                case CapStorageType.InMemoryStorage:
                    @this.UseInMemoryStorage();
                    break;

                case CapStorageType.Mysql:
                    IConfigurationSection mySql = Configuration.GetSection($"ConnectionStrings:MySql");
                    @this.UseMySql(mySql.Value);
                    break;

                //case CapStorageType.SqlServer:
                //    IConfigurationSection sqlServer = Configuration.GetSection($"ConnectionStrings:SqlServer");
                //    @this.UseSqlServer(opt =>
                //    {
                //        opt.ConnectionString = sqlServer.Value;
                //        opt.UseSqlServer2008();
                //    });
                //    break;
                default:
                    break;
                }
            }
            else
            {
                Log.Error($"CAP:DefaultStorage:{capStorageType}配置无效,仅支持InMemoryStorage,Mysql,SqlServer!更多请增加引用,修改配置项代码");
            }

            if (Enum.TryParse(defaultMessageQueue.Value, out CapMessageQueueType capMessageQueueType))
            {
                if (!Enum.IsDefined(typeof(CapMessageQueueType), capMessageQueueType))
                {
                    Log.Error($"CAP配置CAP:DefaultMessageQueue:{defaultMessageQueue.Value}无效");
                }
                IConfigurationSection configurationSection = Configuration.GetSection($"ConnectionStrings:{capMessageQueueType}");

                switch (capMessageQueueType)
                {
                case CapMessageQueueType.InMemoryQueue:
                    @this.UseInMemoryMessageQueue();
                    break;

                case CapMessageQueueType.RabbitMQ:
                    @this.UseRabbitMQ(options =>
                    {
                        options.HostName    = Configuration["CAP:RabbitMQ:HostName"];
                        options.UserName    = Configuration["CAP:RabbitMQ:UserName"];
                        options.Password    = Configuration["CAP:RabbitMQ:Password"];
                        options.VirtualHost = Configuration["CAP:RabbitMQ:VirtualHost"];
                    });
                    break;

                default:
                    break;
                }
            }
            else
            {
                Log.Error($"CAP配置CAP:DefaultMessageQueue:{defaultMessageQueue.Value}无效");
            }

            return(@this);
        }
Esempio n. 2
0
        /// <summary>
        /// 根据配置文件配置Cap
        /// </summary>
        /// <param name="options"></param>
        /// <param name="Configuration"></param>
        /// <returns></returns>
        private static CapOptions UseCapOptions(this CapOptions options)
        {
            var defaultStorage      = Appsettings.CapDefaultStorage;
            var defaultMessageQueue = Appsettings.CapDefaultMessageQueue;

            //配置Cap默认存储类型
            if (Enum.TryParse(defaultStorage, out CapStorageTypeEnums capStorageType))
            {
                if (!Enum.IsDefined(typeof(CapStorageTypeEnums), capStorageType))//枚举中是否存在该类型定义
                {
                    Log.Error($"CAP配置:DefaultStorage:{defaultStorage}无效");
                }

                switch (capStorageType)
                {
                case CapStorageTypeEnums.InMemoryStorage:
                    options.UseInMemoryStorage();
                    break;

                case CapStorageTypeEnums.Mysql:
                    options.UseMySql(opt =>
                    {
                        opt.ConnectionString = Appsettings.MySqlCon;
                        opt.TableNamePrefix  = SystemConst.DbTablePrefix;
                    });
                    break;

                default:
                    break;
                }
            }
            else
            {
                Log.Error($"CAP配置:DefaultStorage:{capStorageType}配置无效,仅支持InMemoryStorage,Mysql!更多请增加引用,修改配置项代码");
            }
            //配置Cap默认消息队列
            if (Enum.TryParse(defaultMessageQueue, out CapMessageQueueTypeEnums capMessageQueueType))
            {
                if (!Enum.IsDefined(typeof(CapMessageQueueTypeEnums), capMessageQueueType))//枚举中是否存在该类型定义
                {
                    Log.Error($"CAP配置:DefaultMessageQueue:{defaultMessageQueue}无效");
                }
                //IConfigurationSection configurationSection = Configuration.GetSection($"ConnectionStrings:{capMessageQueueType}");

                switch (capMessageQueueType)
                {
                case CapMessageQueueTypeEnums.InMemoryQueue:
                    options.UseInMemoryMessageQueue();
                    break;

                case CapMessageQueueTypeEnums.RabbitMQ:
                    options.UseRabbitMQ(options =>
                    {
                        options.HostName    = Appsettings.CapRabbitMq.HostName;
                        options.UserName    = Appsettings.CapRabbitMq.UserName;
                        options.Password    = Appsettings.CapRabbitMq.Password;
                        options.VirtualHost = Appsettings.CapRabbitMq.VirtualHost;
                    });
                    break;

                default:
                    break;
                }
            }
            else
            {
                Log.Error($"CAP配置:DefaultMessageQueue:{defaultMessageQueue}无效");
            }

            return(options);
        }