/// <summary> /// 注入上下文的实例类型 /// </summary> /// <returns></returns> public static IServiceCollection AddEFOption(this IServiceCollection services, Action <EFOptions> action) { EFOptions eFOptions = new EFOptions(); action?.Invoke(eFOptions); if (eFOptions == null) { throw new ArgumentNullException(nameof(action)); } //注入上下文服务扩展 Extension[eFOptions.DbContextType](services); //验证 if (eFOptions.IsOpenMasterSlave && (eFOptions.ReadOnlyConnectionString == null || eFOptions.ReadOnlyConnectionString.Count() <= 0)) { throw new ArgumentException("检测到开启了读写分离但是未指定只读的连接字符串!"); } //写入连接字符串的线程安全集合 if (eFOptions.IsOpenMasterSlave) { var slaveDbConnectionFactory = services.BuildServiceProvider().GetRequiredService <ISlaveDbConnectionFactory>(); SlavePools.slaveConnec.TryAdd(eFOptions.DbContextType, slaveDbConnectionFactory.Get(eFOptions)); } //注入配置服务 services.Add(new ServiceDescriptor(MergeNamedType.Merge(eFOptions.DbContextType.Name, typeof(EFOptions)), serviceProvider => eFOptions, ServiceLifetime.Singleton)); return(services); }
/// <summary> /// 注入服务配置 /// </summary> /// <param name="services"></param> /// <returns></returns> public static IServiceCollection AddMongoContext <TMongoContext>(this IServiceCollection services, Action <TMongoContext> options) where TMongoContext : MongoContext, new() { TMongoContext mongoContext = new TMongoContext(); options?.Invoke(mongoContext); //注入配置服务 services.Add(new ServiceDescriptor(MergeNamedType.Merge(typeof(TMongoContext).Name, typeof(TMongoContext)), serviceProvider => mongoContext, ServiceLifetime.Singleton)); return(services); }
/// <summary> /// 获取配置信息 /// </summary> /// <param name="key"></param> /// <returns></returns> public Task <WebSocketOption> GetAsync(string key) { if (cache.TryGetValue(key, out var webSocketOption)) { return(Task.FromResult(webSocketOption)); } var option = serviceProvider.GetService(MergeNamedType.Get(key)) as WebSocketOption; if (option == null) { return(default);
public DefaultMongoGridFSInfrastructure(MongoContextOptions <TMongoContext> _mongoContextOptions, IMongoClientFactory _mongoClientFactory, IServiceProvider serviceProvider) { currentContextOptions = _mongoContextOptions; //获取mongo客户端信息 var mongoClientInfo = _mongoClientFactory.GetMongoClient <TMongoContext>(); //获取客户端 currentMongoClient = mongoClientInfo.Item1; //获取当前上下文信息 currentMongoContext = serviceProvider.GetService(MergeNamedType.Get(typeof(TMongoContext).Name)) as TMongoContext; //构建GridFS对象 gridFSBucket = Build(currentMongoClient.GetDatabase(mongoClientInfo.Item2.DataBase), currentMongoContext.BucketName); }
/// <summary> /// 注入服务 /// </summary> /// <param name="services"></param> /// <returns></returns> public static IServiceCollection AddNarutoWebSocket <TService>(this IServiceCollection services, Action <WebSocketOption> option) where TService : NarutoWebSocketService { if (services.BuildServiceProvider().GetService <IWebSocketOptionFactory>() == null) { services.AddSingleton(typeof(IGroupStorage <>), typeof(InMemoryGroupStorage <>)); services.AddSingleton(typeof(IWebSocketClientStorage <>), typeof(InMemoryWebSocketClientStorage <>)); services.AddSingleton(typeof(IClientSend <>), typeof(ClientSend <>)); services.AddSingleton(typeof(IAllClient <>), typeof(AllClient <>)); services.AddSingleton(typeof(IOtherClient <>), typeof(OtherClient <>)); services.AddSingleton(typeof(ICurrentClient <>), typeof(CurrentClient <>)); services.AddSingleton(typeof(IGroupClient <>), typeof(GroupClient <>)); services.AddSingleton(typeof(IClusterAllClient <>), typeof(AllClient <>)); services.AddSingleton(typeof(IClusterOtherClient <>), typeof(OtherClient <>)); services.AddSingleton(typeof(IClusterCurrentClient <>), typeof(CurrentClient <>)); services.AddSingleton(typeof(IClusterGroupClient <>), typeof(GroupClient <>)); services.AddSingleton <IEventBusProxy, EventBusProxy>(); services.AddSingleton <IMessageReviceHandler, MessageReviceHandler>(); services.AddSingleton <IWebSocketOptionFactory, WebSocketOptionFactory>(); services.AddScoped(typeof(CurrentContext <>)); services.AddScoped(typeof(IReplyClient <>), typeof(ReplyClient <>)); } services.AddScoped <TService>(); //获取配置 WebSocketOption webSocketOption = new WebSocketOption(); option?.Invoke(webSocketOption); //匹配是否存在 if (!TenantPathCache.Match(webSocketOption.Path)) { //获取服务类型 webSocketOption.ServiceType = typeof(TService); //添加进路由缓存 TenantPathCache.Add(webSocketOption.Path, webSocketOption.ServiceType); //注入配置服务 services.Add(new ServiceDescriptor(MergeNamedType.Merge(webSocketOption.Path.ToString(), typeof(WebSocketOption)), serviceProvider => webSocketOption, ServiceLifetime.Singleton)); } return(services); }
/// <summary> /// 获取客户端 /// </summary> /// <typeparam name="TMongoContext"></typeparam> /// <returns></returns> public Tuple <IMongoClient, MongoContext> GetMongoClient <TMongoContext>() where TMongoContext : MongoContext { var contextType = typeof(TMongoContext); //从内存中读取mongodb客户端 var mongoClientInfo = MongoClientPool.Where(a => a.Key == contextType).Select(a => a.Value).FirstOrDefault(); if (mongoClientInfo != null) { return(mongoClientInfo); } //获取上下文信息 var mongoContextInfo = serviceProvider.GetService(MergeNamedType.Get(contextType.Name)) as TMongoContext; mongoContextInfo.CheckNull(); //实例化mongo客户端信息 mongoClientInfo = new Tuple <IMongoClient, MongoContext>(new MongoClient(mongoContextInfo.ConnectionString), mongoContextInfo); //存储进字典 MongoClientPool.TryAdd(contextType, mongoClientInfo); return(mongoClientInfo); }
public EFOptions Get(Type dbContext) { return(serviceProvider.GetService(MergeNamedType.Get(dbContext.Name)) as EFOptions); }
public EFOptions Get <TDbContext>() where TDbContext : DbContext { return(serviceProvider.GetService(MergeNamedType.Get(typeof(TDbContext).Name)) as EFOptions); }