/// <summary> /// 添加Mongo数据库配置 /// </summary> /// <param name="servers"></param> /// <param name="connectName"></param> /// <param name="userName"></param> /// <param name="password"></param> /// <param name="maxConnectionPoolSize"></param> public static void Configure(List<Uri> servers, string connectName, string userName = "", string password = "", int maxConnectionPoolSize = MaxConnectionPoolSize) { if (Configures.ContainsKey(connectName) && Configures[connectName] != null) return; lock (LockObj) { if (Configures.ContainsKey(connectName) && Configures[connectName] != null) return; var config = new MongoDbConfigure { Servers = servers, MongoClientSettings = new MongoClientSettings() { MaxConnectionPoolSize = maxConnectionPoolSize > 0 ? maxConnectionPoolSize : MaxConnectionPoolSize, WaitQueueSize = 5000, Servers = servers.Select(uri => new MongoServerAddress(uri.Host, uri.Port)) } }; if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) config.MongoClientSettings.Credentials = new List<MongoCredential> { MongoCredential.CreateMongoCRCredential("admin", userName, password) }; config.MongoClient = new MongoClient(config.MongoClientSettings); Configures[connectName] = config; } }
/// <summary> /// GetMongoClientSettings /// </summary> /// <param name="configure"></param> /// <returns></returns> private static MongoClientSettings GetMongoClientSettings(MongoDbConfigure configure) { var setting = new MongoClientSettings { MaxConnectionPoolSize = configure.MaxConnectionPoolSize > 0 ? configure.MaxConnectionPoolSize : MaxConnectionPoolSize, WaitQueueSize = 5000, Servers = configure.Servers.Select(uri => new MongoServerAddress(uri.Host, uri.Port)) }; if (!string.IsNullOrEmpty(configure.UserName) && !string.IsNullOrEmpty(configure.Password)) { setting.Credentials = GetMongoCredential(configure.UserName, configure.Password, configure.CredentialDataBase, configure.Credential); } return setting; }
/// <summary> /// 配置文件 /// </summary> /// <param name="servers"></param> /// <param name="connectName"></param> /// <param name="userName"></param> /// <param name="password"></param> /// <param name="credentialDataBase"></param> /// <param name="maxConnectionPoolSize"></param> /// <param name="credential"></param> public static void Configure(IEnumerable<Uri> servers, string connectName, string userName, string password, string credentialDataBase, int maxConnectionPoolSize = MaxConnectionPoolSize, MongoDbCredential credential = MongoDbCredential.ScramSha1) { if (_configures == null) _configures = new ConcurrentDictionary<string, MongoDbConfigure>(); if (!_configures.ContainsKey(connectName)) { _configures[connectName] = new MongoDbConfigure { Servers = servers, ConnectionName = connectName, UserName = userName, Password = password, CredentialDataBase = credentialDataBase, Credential = credential, MaxConnectionPoolSize = maxConnectionPoolSize }; } }