示例#1
0
        /// <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);
        }
示例#2
0
        /// <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);
        }
示例#3
0
        /// <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);
        }