コード例 #1
0
 /// <summary>
 /// 静态构造方法,初始化链接池管理对象
 /// </summary>
 protected IMongoDatabase ShareMongoDb(string dbConfigSelectionKey)
 {
     if (!dbDic.ContainsKey(dbConfigSelectionKey))
     {
         lock (lockObj)
         {
             if (!dbDic.ContainsKey(dbConfigSelectionKey))
             {
                 MongoConfig _mongoConfig = MongoConfig.GetConfig(dbConfigSelectionKey);
                 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用户密码
                 {
                     MongoCredential        mongoCredential     = MongoCredential.CreateCredential(_mongoConfig.DefaultDb, _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]);
 }
コード例 #2
0
ファイル: MongoLogConfig.cs プロジェクト: Alex1119/Web.Api
        public static MongoConfig GetConfig(string sectionName)
        {
            MongoConfig section = (MongoConfig)ConfigurationManager.GetSection(sectionName);

            if (section == null)
            {
                throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
            }
            return(section);
        }