示例#1
0
        public static void AddTransPortService(this IServiceCollection services)
        {
            Func <IServiceProvider, Func <string, ITransPortService> > func = serviceProvider => (name) =>
            {
                if (TransPortServiceDependencyInjection.GetFuncs().Any())
                {
                    foreach (var item in TransPortServiceDependencyInjection.GetFuncs())
                    {
                        try
                        {
                            var persistence = item.Invoke(serviceProvider, name);
                            if (persistence != null)
                            {
                                return(persistence);
                            }
                        }
                        catch
                        {
                        }
                    }
                }
                throw new Exception($"not found service by name {name}");
            };


            services.AddSingleton <Func <string, ITransPortService> >(func);
        }
示例#2
0
        public static void AddLogMongodb(this IServiceCollection services, Action <MongoDbOptions> action = null)
        {
            MongoDbOptions options = new MongoDbOptions();

            action?.Invoke(options);

            services.AddSingleton(options);
            services.AddSingleton <IMongodbTransPortService, LogMongodbTransPortService>();
            TransPortServiceDependencyInjection.AddFunc((serviceProvider, name) =>
            {
                if (name.Equals(MongodbConstant.MONGODBNAME, StringComparison.OrdinalIgnoreCase))
                {
                    return(serviceProvider.GetService <IMongodbTransPortService>());
                }
                return(null);
            });
        }
示例#3
0
        public static void AddElasticsearch(this IServiceCollection services, Action <ElasticSearchOptions> action = null)
        {
            ElasticSearchOptions options = new ElasticSearchOptions();

            action?.Invoke(options);

            services.AddSingleton(options);
            services.AddSingleton <IEsClientProvider, EsClientProvider>();
            services.AddSingleton <IElasticSearchTransPortService, ElasticSearchTransPortService>();


            TransPortServiceDependencyInjection.AddFunc((serviceProvider, name) =>
            {
                if (name.Equals(ElasticsearchConstant.ELASTICSEARCHNAME, StringComparison.OrdinalIgnoreCase))
                {
                    return(serviceProvider.GetService <IElasticSearchTransPortService>());
                }
                return(null);
            });
        }
示例#4
0
        public static void AddFreeSql(this IServiceCollection services, Action <FreeSqlOptions> action = null)
        {
            FreeSqlOptions freeSqlOptions = new FreeSqlOptions();

            action?.Invoke(freeSqlOptions);
            services.AddSingleton(freeSqlOptions);
            IdleBus <IFreeSql> ib = new IdleBus <IFreeSql>(freeSqlOptions.TimeSpan);

            //Dictionary<string, IFreeSql> dic = new Dictionary<string, IFreeSql>();
            foreach (var item in freeSqlOptions.FreeSqlDbs)
            {
                var isRegisterSucess = ib.TryRegister(item.Name, () =>
                {
                    var freesql = new FreeSqlBuilder()
                                  .UseConnectionString(item.DataType, item.ConnectString)
                                  .UseAutoSyncStructure(item.IsAutoSyncStructure) //自动同步实体结构到数据库
                                  .UseNoneCommandParameter(item.IsNoneCommandParameter)
                                  .UseLazyLoading(item.IsLazyLoading)
                                  .UseNameConvert(item.NameConvertType)
                                  .UseMonitorCommand(item.Executing, item.Executed).Build();
                    freesql.UseJsonMap();
                    return(freesql);
                });
                if (!isRegisterSucess)
                {
                    throw new Exception($"{item.Name}数据库注入失败");
                }
            }

            services.AddSingleton <IFreeSqlTransPortService, FreeSqlTransPortService>();
            services.AddSingleton(ib);
            TransPortServiceDependencyInjection.AddFunc((serviceProvider, name) =>
            {
                if (name.Equals(FreeSqlConstant.FREESQLNAME, StringComparison.OrdinalIgnoreCase))
                {
                    return(serviceProvider.GetService <IFreeSqlTransPortService>());
                }
                return(null);
            });
        }