Ejemplo n.º 1
0
 /// <summary>
 /// 静态构造方法,初始化链接池管理对象
 /// </summary>
 /// <param name="dbConfigSelectionKey">连接池对象的Key</param>
 /// <param name="_mongoConfig">MongoDB连接对象</param>
 protected IMongoDatabase ShareMongoDb(string dbConfigSelectionKey, MongoConfig _mongoConfig)
 {
     if (!dbDic.ContainsKey(dbConfigSelectionKey))
     {
         lock (lockObj)
         {
             if (!dbDic.ContainsKey(dbConfigSelectionKey) && _mongoConfig != null)
             {
                 var logSetting = new MongoClientSettings
                 {
                     Server = new MongoServerAddress(_mongoConfig.ServerConStr, _mongoConfig.ServerPort),
                     MaxConnectionPoolSize = _mongoConfig.MaxConnectionPoolSize,
                     MaxConnectionIdleTime = TimeSpan.FromSeconds(_mongoConfig.MaxConnectionIdleTime),
                     MaxConnectionLifeTime = TimeSpan.FromSeconds(_mongoConfig.MaxConnectionLifeTime),
                     ConnectTimeout        = TimeSpan.FromSeconds(_mongoConfig.ConnectTimeout),
                     WaitQueueSize         = _mongoConfig.WaitQueueSize,
                     SocketTimeout         = TimeSpan.FromSeconds(_mongoConfig.SocketTimeout),
                     WaitQueueTimeout      = TimeSpan.FromSeconds(_mongoConfig.WaitQueueTimeout)
                 };
                 if (!string.IsNullOrEmpty(_mongoConfig.UserName) && !string.IsNullOrEmpty(_mongoConfig.PassWord))//设置MongoDb用户密码
                 {
                     string authDB = _mongoConfig.AuthDb;
                     if (string.IsNullOrEmpty(authDB))
                     {
                         authDB = _mongoConfig.DefaultDb;
                     }
                     MongoCredential        mongoCredential     = MongoCredential.CreateCredential(authDB, _mongoConfig.UserName, _mongoConfig.PassWord);
                     List <MongoCredential> mongoCredentialList = new List <MongoCredential>();
                     mongoCredentialList.Add(mongoCredential);
                     logSetting.Credentials = mongoCredentialList;
                 }
                 var            logClient = new MongoClient(logSetting);
                 IMongoDatabase db        = logClient.GetDatabase(_mongoConfig.DefaultDb);
                 dbDic.Add(dbConfigSelectionKey, db);
             }
         }
     }
     return(dbDic[dbConfigSelectionKey]);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 创建Mongo仓储对象
 /// </summary>
 /// <param name="dbSelectionKey">仓储键名</param>
 /// <param name="collectionName">表名</param>
 /// <param name="mongoConfig">MongoDB服务器配置类</param>
 public DoMongoRepostory(string dbSelectionKey, string collectionName, MongoConfig mongoConfig)
 {
     this.Collection = ShareMongoDb(dbSelectionKey, mongoConfig).GetCollection <TAggregate>(collectionName);
 }