/// <summary> /// 注入csredis /// </summary> /// <param name="services"></param> public static void AddRedisService(this IServiceCollection services) { // 普遍模式 var csredis = new CSRedis.CSRedisClient(AppSetting.GetConnStrings("RedisCon").ToString()); // 初始化redisHelper RedisHelper.Initialization(csredis); // 注入Mvc分布式缓存CsRedisCache services.AddSingleton <IDistributedCache>( new Microsoft.Extensions.Caching.Redis.CSRedisCache(RedisHelper.Instance)); services.AddSingleton <ICsRedisRepository, CsRedisRepository>(); }
/// <summary> /// 注入cap /// </summary> /// <param name="services"></param> public static void AddCapEvent(this IServiceCollection services) { #region cap简介 // CAP 是一个在分布式系统中实现事件总线及最终一致性(分布式事务) // 具有轻量级,高性能,易使用等特点。 // 具有 Event Bus 的所有功能,并且CAP提供了更加简化的方式来处理EventBus中的发布 / 订阅 // 具有消息持久化的功能 // 实现了分布式事务中的最终一致性, #endregion services.AddCap(x => { x.UseDashboard(); string dbAble = AppSetting.GetConnStrings("DbAllow"); if (!string.IsNullOrEmpty(dbAble)) { switch (dbAble) { case "MsSqlCon": //x.UseKafka(AppSetting.GetConnStrings("MysqlCon")); break; case "PostgreSql": x.UsePostgreSql(AppSetting.GetConnStrings("PostgreSqlCon")); break; default: x.UseMySql(AppSetting.GetConnStrings("MysqlCon")); break; } } x.UseKafka(AppSetting.GetConnStrings("KafkaCon")); x.FailedRetryInterval = 5; x.FailedRetryCount = 2; #region 注入Consul //x.UseDiscovery(d => //{ // d.DiscoveryServerHostName = "localhost"; // d.DiscoveryServerPort = 8500; // d.CurrentNodeHostName = "localhost"; // d.CurrentNodePort = 5800; // d.NodeId = 1; // d.NodeName = "CAP No.1 Node"; //}); #endregion }); }
/// <summary> /// 注入freesql /// </summary> /// <param name="services"></param> public static void AddFreeSqlService(this IServiceCollection services) { IFreeSql freeSql = new FreeSqlBuilder() // 添加连接字符串和类型 .UseConnectionString(DataType.MySql, AppSetting.GetConnStrings("MysqlCon")) // 自动同步实体结构到数据库 // 注意:不要在生产环境随意开启 .UseAutoSyncStructure(false) // 开启延时加载 .UseLazyLoading(true) .Build(); services.AddSingleton <IFreeSql>(freeSql); //services.AddFreeRepository(); //services.AddScoped<UnitOfWorkManager>(); }
/// <summary> /// 注入dbcontext /// </summary> /// <param name="services"></param> public static void AddDataDbContext(this IServiceCollection services) { services.AddDbContext <DataDbContext>(options => { switch (AppSetting.GetConnStrings("DbAllow")) { case "MsSqlCon": //options.UseSqlServer(AppSetting.GetConnStrings("MSSQLCon")); break; default: options.UseMySQL(AppSetting.GetConnStrings("MysqlCon")); break; } }); }
/// <summary> /// 注入EasyNetQ /// </summary> /// <param name="services"></param> public static void AddEasyNetQService(this IServiceCollection services) { services.AddSingleton(RabbitHutch.CreateBus(AppSetting.GetConnStrings("MqCon"))); }