예제 #1
0
        public DefaultConnectionFactory(IOptions <MongoDBSetting> settings)
        {
            if (settings == null || settings.Value == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            _settings       = settings.Value;
            _clientAccessor = new Lazy <IMongoClient>(BuildClient, LazyThreadSafetyMode.ExecutionAndPublication);
        }
예제 #2
0
        public MongoDBRepository(IOptions <MongoDBSetting> databaseProvider)
        {
            _databaseProvider = databaseProvider.Value;
            var client = new MongoClient(databaseProvider.Value.ConnectionString);

            if (client != null)
            {
                _database = client.GetDatabase(databaseProvider.Value.DatabaseName);
            }
        }
예제 #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped <IContactApplyRequestRepository, MongoContactApplyRequestRepository>();
            services.AddScoped <IContactBookRepository, MongoContactBookRepository>();
            services.AddScoped <IUserService, UserService>();
            services.AddScoped <UserProfileChangedEventHandler>();

            //services.Configure<MongoDBSetting>(Configuration.GetSection("MongoSettings"));

            var            mongoSettingsConfig    = Configuration.GetSection(GlobalObject.Namespace_MongoSettings);
            var            strMongoSettingsConfig = mongoSettingsConfig.GetValue(GlobalObject.Namespace_DefaultKey, GlobalObject.DefaultConfigValue);
            MongoDBSetting objMongoSettings       = JsonConvert.DeserializeObject <MongoDBSetting>(strMongoSettingsConfig);

            services.Configure <MongoDBSetting>(opt =>
            {
                opt.DataBase = objMongoSettings.DataBase;
                opt.UserName = objMongoSettings.UserName;
                opt.Password = objMongoSettings.Password;
                opt.Services = objMongoSettings.Services;
            });

            services.AddSingleton <ContactContext>();

            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(opt =>
            {
                opt.RequireHttpsMetadata = false;
                opt.Audience             = "contact_api";
                opt.Authority            = "http://localhost";
                opt.SaveToken            = true;
            });

            //services.Configure<ServiceDiscoveryOptions>(Configuration.GetSection("ServiceDiscovery"));
            var serviceDiscoveryConfig    = Configuration.GetSection(GlobalObject.Namespace_ServiceDiscovery);
            var strServiceDiscoveryConfig = serviceDiscoveryConfig.GetValue(GlobalObject.Namespace_DefaultKey, GlobalObject.DefaultConfigValue);
            ServiceDiscoveryOptions objServiceDiscovery = JsonConvert.DeserializeObject <ServiceDiscoveryOptions>(strServiceDiscoveryConfig);

            services.Configure <ServiceDiscoveryOptions>(opt =>
            {
                opt.Consul          = objServiceDiscovery.Consul;
                opt.ServiceName     = objServiceDiscovery.ServiceName;
                opt.UserServiceName = objServiceDiscovery.UserServiceName;
            });

            services.AddSingleton <IDnsQuery>(p =>
            {
                var serviceConfiguration = p.GetRequiredService <IOptions <ServiceDiscoveryOptions> >().Value;
                return(new LookupClient(serviceConfiguration.Consul.DnsEndpoint.ToIPEndPoint()));
            });

            //services.AddSingleton(new HttpClient());
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton(typeof(ResilienceHttpClientFactory), sp =>
            {
                var logger = sp.GetRequiredService <ILogger <ResilientHttpClient> >();
                var httpContextAccessor             = sp.GetRequiredService <IHttpContextAccessor>();
                var retryCount                      = 5;
                var exceptionsAllowedBeforeBreaking = 5;
                return(new ResilienceHttpClientFactory(logger, httpContextAccessor, retryCount, exceptionsAllowedBeforeBreaking));
            });
            services.AddSingleton <IHttpClient>(sp => sp.GetRequiredService <ResilienceHttpClientFactory>().GetResilientHttpClient());

            services.AddSingleton <IConsulClient>(p => new ConsulClient(cfg =>
            {
                var serviceConfiguration = p.GetRequiredService <IOptions <ServiceDiscoveryOptions> >().Value;

                if (!string.IsNullOrEmpty(serviceConfiguration.Consul.HttpEndpoint))
                {
                    // if not configured, the client will use the default value "127.0.0.1:8500"
                    cfg.Address = new Uri(serviceConfiguration.Consul.HttpEndpoint);
                }
            }));
            services.AddSingleton <IHostedService, HostedService>();
            services.AddSingleton <ElasticClient>(sp => ContactSearchConfig.GetClient());

            string connStr = Configuration.GetValue("MysqlContact", GlobalObject.DefaultConfigValue);

            var capDiscoveryConfig           = Configuration.GetSection(GlobalObject.Namespace_CAPDiscovery);
            var strCAPDiscoveryConfig        = capDiscoveryConfig.GetValue(GlobalObject.Namespace_DefaultKey, GlobalObject.DefaultConfigValue);
            DiscoveryOptions objCAPDiscovery = JsonConvert.DeserializeObject <DiscoveryOptions>(strCAPDiscoveryConfig);

            services.AddCap(opt =>
            {
                opt.UseMySql(connStr)
                .UseRabbitMQ("localhost")
                .UseDashboard();
                opt.UseDiscovery(d =>
                {
                    d.DiscoveryServerHostName = objCAPDiscovery.DiscoveryServerHostName;
                    d.DiscoveryServerPort     = objCAPDiscovery.DiscoveryServerPort;
                    d.CurrentNodeHostName     = objCAPDiscovery.CurrentNodeHostName;
                    d.CurrentNodePort         = objCAPDiscovery.CurrentNodePort;
                    d.NodeId   = objCAPDiscovery.NodeId;
                    d.NodeName = objCAPDiscovery.NodeName;
                });
            });
            services.AddMvc(opt => opt.EnableEndpointRouting = false);
        }