public MongoRepositoryBase(IOptions <MongoConnectionConfig> setting) { _mongoConnection = setting.Value; Connect(); _serializerRegistry = BsonSerializer.SerializerRegistry; _documentSerializer = _serializerRegistry.GetSerializer <TEntity>(); }
/// <summary> /// 获得MongoUri /// </summary> /// <returns></returns> public static string ToUri(this MongoConnectionConfig config) { var uri = string.Empty; uri = "mongodb://" + config.Host + ":" + config.Port; return(uri); }
/// <summary> /// 初始化(修改) /// </summary> /// <param name="connectionName"></param> public FrmConnectionMgr(string connectionName) { InitializeComponent(); foreach (var item in Enum.GetValues(typeof(EnumMgr.StorageEngineType))) { cmbStorageEngine.Items.Add(item); cmbStorageEngine.SelectedIndex = 0; } OldConnectionName = connectionName; //Modify Mode ModifyConn = MongoConnectionConfig.MongoConfig.ConnectionList[connectionName]; UiBinding.TryUpdateForm(ModifyConn, Controls); if (ModifyConn.AuthMechanism == ConstMgr.MONGODB_CR) { radMONGODB_CR.Checked = true; } if (ModifyConn.AuthMechanism == ConstMgr.MONGODB_X509) { radMONGODB_X509.Checked = true; } if (ModifyConn.AuthMechanism == ConstMgr.SCRAM_SHA_1) { radSCRAM_SHA_1.Checked = true; } foreach (var item in ModifyConn.ReplsetList) { lstHost.Items.Add(item); } cmbStorageEngine.SelectedIndex = ModifyConn.StorageEngine == EnumMgr.StorageEngineType.MmaPv1 ? 0 : 1; cmdAdd.Text = GuiConfig.IsUseDefaultLanguage ? "Modify" : GuiConfig.GetText("Common.Modify"); }
/// <summary> /// 获取实例节点 /// 这里将形成左侧的树型目录 /// </summary> /// <param name="mongoConnKey"></param> /// <param name="config">由于是结构体,必须ref</param> /// <param name="mongoConn"></param> /// <param name="mMasterServerInstace"></param> /// <param name="userList"></param> /// <returns></returns> private static TreeNode GetInstanceNode(string mongoConnKey, ref MongoConnectionConfig config, MongoClient mongoClient) { //无论如何,都改为主要服务器读优先 var svrInstanceNode = new TreeNode(); var connSvrKey = mongoConnKey + "/" + mongoConnKey; svrInstanceNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.WebServer; svrInstanceNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.WebServer; svrInstanceNode.Text = "Server"; if (!string.IsNullOrEmpty(config.UserName) & (!string.IsNullOrEmpty(config.Password))) { //是否是认证模式,应该取决于服务器! config.AuthMode = true; } //获取ReadOnly config.IsReadOnly = false; if (!string.IsNullOrEmpty(config.DataBaseName)) { //单数据库模式 var mongoSingleDbNode = FillDataBaseInfoToTreeNode(config.DataBaseName, mongoConnKey + "/" + mongoConnKey, null); mongoSingleDbNode.Tag = ConstMgr.SingleDatabaseTag + ":" + connSvrKey + "/" + config.DataBaseName; mongoSingleDbNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Database; mongoSingleDbNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.Database; svrInstanceNode.Nodes.Add(mongoSingleDbNode); svrInstanceNode.Tag = ConstMgr.SingleDbServerTag + ":" + connSvrKey; } else { var setting = RuntimeMongoDbContext.CreateMongoClientSettingsByConfig(ref config); var client = new MongoClient(setting); var databaseNameList = GetConnectionInfo.GetDatabaseList(client); foreach (var strDbName in databaseNameList) { TreeNode mongoDbNode; try { var dbName = strDbName.GetElement("name").Value.ToString(); mongoDbNode = FillDataBaseInfoToTreeNode(dbName, connSvrKey, client); mongoDbNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.Database; mongoDbNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Database; svrInstanceNode.Nodes.Add(mongoDbNode); } catch (Exception ex) { Utility.ExceptionDeal(ex, strDbName + "Exception", strDbName + "Exception"); mongoDbNode = new TreeNode(strDbName + " (Exception)") { ImageIndex = (int)GetSystemIcon.MainTreeImageType.Database, SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Database }; svrInstanceNode.Nodes.Add(mongoDbNode); } } } svrInstanceNode.Tag = ConstMgr.ServerTag + ":" + mongoConnKey + "/" + mongoConnKey; return(svrInstanceNode); }
/// <summary> /// 刷新配置文件副本状态 /// </summary> /// <param name="newConfig"></param> public static void RefreshConnectionConfig(MongoConnectionConfig newConfig) { MongoConnectionConfig.MongoConfig.ConnectionList[newConfig.ConnectionName] = newConfig; MongoConnectionConfig.MongoConfig.SaveMongoConfig(); RuntimeMongoDbContext.MongoConnSvrLst.Remove(newConfig.ConnectionName); RuntimeMongoDbContext.MongoConnSvrLst.Add(RuntimeMongoDbContext.CurrentMongoConnectionconfig.ConnectionName, RuntimeMongoDbContext.CreateMongoServer(ref newConfig)); }
public ContactContext(IOptionsSnapshot <MongoConnectionConfig> settings) { _connectionConfig = settings.Value; var client = new MongoClient(_connectionConfig.ConnectionString); if (client != null) { _mongoDatabase = client.GetDatabase(_connectionConfig.Database); } }
/// <summary> /// 使用字符串连接来填充 /// </summary> /// <remarks>http://www.mongodb.org/display/DOCS/Connections</remarks> /// <param name="config"></param> public static string FillConfigWithConnectionString(ref MongoConnectionConfig config) { var connectionString = config.ConnectionString; //mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]] try { var mongourl = MongoUrl.Create(connectionString); config.DataBaseName = mongourl.DatabaseName; if (mongourl.Username != null) { config.UserName = mongourl.Username; config.Password = mongourl.Password; //config.LoginAsAdmin = mongourl.Admin; } config.Host = mongourl.Server.Host; config.Port = mongourl.Server.Port; if (mongourl.ReadPreference != null) { config.ReadPreference = mongourl.ReadPreference.ToString(); } config.WriteConcern = mongourl.GetWriteConcern(true).ToString(); config.WaitQueueSize = mongourl.WaitQueueSize; config.WtimeoutMs = (int)mongourl.WaitQueueTimeout.TotalMilliseconds; config.IsUseDefaultSetting = false; config.SocketTimeoutMs = (int)mongourl.SocketTimeout.TotalMilliseconds; config.ConnectTimeoutMs = (int)mongourl.ConnectTimeout.TotalMilliseconds; config.ReplSetName = mongourl.ReplicaSetName; config.ReplsetList = new List <string>(); if (!string.IsNullOrEmpty(config.ReplSetName)) { foreach (var item in mongourl.Servers) { config.ReplsetList.Add(item.Host + (item.Port == 0 ? string.Empty : ":" + item.Port)); } } return(string.Empty); } catch (FormatException ex) { return(ex.ToString()); } catch (Exception ex) { return(ex.ToString()); } }
/// <summary> /// 初始化(修改) /// </summary> /// <param name="connectionName"></param> public FrmConnectionMgr(string connectionName) { InitializeComponent(); foreach (var item in Enum.GetValues(typeof(EnumMgr.StorageEngineType))) { cmbStorageEngine.Items.Add(item); cmbStorageEngine.SelectedIndex = 0; } OldConnectionName = connectionName; //Modify Mode ModifyConn = MongoConnectionConfig.MongoConfig.ConnectionList[connectionName]; UiBinding.TryUpdateForm(ModifyConn, Controls); foreach (var item in ModifyConn.ReplsetList) { lstHost.Items.Add(item); } cmbStorageEngine.SelectedIndex = ModifyConn.StorageEngine == EnumMgr.StorageEngineType.MmaPv1 ? 0 : 1; cmdAdd.Text = GuiConfig.IsUseDefaultLanguage ? "Modify" : GuiConfig.GetText(TextType.CommonModify); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cmdFromUriString_Click(object sender, EventArgs e) { var uri = MyMessageBox.ShowInput("MongoConnectionUri", "Uri"); MongoConnectionConfig ModifyConn = new MongoConnectionConfig(); ModifyConn.ConnectionString = uri; var strException = MongoHelper.FillConfigWithConnectionString(ref ModifyConn); if (strException != string.Empty) { MyMessageBox.ShowMessage("Url Exception", "Url Formation,please check it", strException); return; } //这里不能使用 :,会出现问题 var ConnectionName = ModifyConn.Host + "_" + ModifyConn.Port; ModifyConn.ConnectionName = ConnectionName; MongoConnectionConfig.MongoConfig.ConnectionList.Add(ConnectionName, ModifyConn); RefreshConnection(); }
/// <summary> /// 添加链接 /// </summary> /// <param name="con"></param> /// <returns></returns> public Boolean AddConnection(MongoConnectionConfig con) { ConnectionList.Add(con.ConnectionName, con); return true; }
/// <summary> /// </summary> /// <param name="config"></param> public FrmReplsetMgr(ref MongoConnectionConfig config) { InitializeComponent(); RuntimeMongoDbContext.CurrentMongoConnectionconfig = config; }
/// <summary> /// 添加链接 /// </summary> /// <param name="con"></param> /// <returns></returns> public Boolean AddConnection(MongoConnectionConfig con) { ConnectionList.Add(con.ConnectionName, con); return(true); }
/// <summary> /// 获取实例节点 /// 这里将形成左侧的树型目录 /// </summary> /// <param name="mongoConnKey"></param> /// <param name="config">由于是结构体,必须ref</param> /// <param name="mongoConn"></param> /// <param name="mMasterServerInstace"></param> /// <param name="userList"></param> /// <returns></returns> private static TreeNode GetInstanceNode(string mongoConnKey, ref MongoConnectionConfig config, MongoClient mongoClient) { //无论如何,都改为主要服务器读优先 var svrInstanceNode = new TreeNode(); var connSvrKey = mongoConnKey + "/" + mongoConnKey; svrInstanceNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.WebServer; svrInstanceNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.WebServer; svrInstanceNode.Text = "Server"; if (!string.IsNullOrEmpty(config.UserName) & !string.IsNullOrEmpty(config.Password)) { //是否是认证模式,应该取决于服务器! config.AuthMode = true; } //获取ReadOnly config.IsReadOnly = false; if (!string.IsNullOrEmpty(config.DataBaseName) && (!config.DataBaseName.Equals(ConstMgr.DatabaseNameAdmin))) { //单数据库模式 var mongoSingleDbNode = FillDataBaseInfoToTreeNode(config.DataBaseName, mongoConnKey + "/" + mongoConnKey, mongoClient); mongoSingleDbNode.Tag = ConstMgr.SingleDatabaseTag + ":" + connSvrKey + "/" + config.DataBaseName; mongoSingleDbNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Database; mongoSingleDbNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.Database; svrInstanceNode.Nodes.Add(mongoSingleDbNode); svrInstanceNode.Tag = ConstMgr.SingleDbServerTag + ":" + connSvrKey; } else { var setting = RuntimeMongoDbContext.CreateMongoClientSettingsByConfig(ref config); var client = new MongoClient(setting); //这里MongoServerInstanceType和SvrRoleType的概念有些重叠 client.GetServer().Connect(); if (!string.IsNullOrEmpty(client.GetServer().ReplicaSetName)) { //如果是副本的话Instance则无效,Instance(s)有效 config.ServerRole = MongoConnectionConfig.SvrRoleType.ReplsetSvr; } else { if (client.GetServer().Instance.InstanceType == MongoServerInstanceType.ShardRouter) { //无法获得数据库列表 config.ServerRole = MongoConnectionConfig.SvrRoleType.ShardSvr; } } var databaseNameList = ConnectionInfo.GetDatabaseInfoList(client); foreach (var DbPropertyDoc in databaseNameList) { TreeNode mongoDbNode; try { var dbName = DbPropertyDoc.GetElement("name").Value.ToString(); mongoDbNode = FillDataBaseInfoToTreeNode(dbName, connSvrKey, client); mongoDbNode.ImageIndex = (int)GetSystemIcon.MainTreeImageType.Database; mongoDbNode.SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Database; svrInstanceNode.Nodes.Add(mongoDbNode); } catch (Exception) { //Utility.ExceptionDeal(ex, strDbName + "Exception", strDbName + "Exception"); mongoDbNode = new TreeNode(DbPropertyDoc.GetElement("name").Value.ToString() + "(Exception)") { ImageIndex = (int)GetSystemIcon.MainTreeImageType.Database, SelectedImageIndex = (int)GetSystemIcon.MainTreeImageType.Database }; svrInstanceNode.Nodes.Add(mongoDbNode); } } } svrInstanceNode.Tag = ConstMgr.ServerTag + ":" + mongoConnKey + "/" + mongoConnKey; return(svrInstanceNode); }
/// <summary> /// 添加链接 /// </summary> /// <param name="con"></param> /// <returns></returns> public static Boolean AddConnection(MongoConnectionConfig con) { SystemManager.config.ConnectionList.Add(con.ConnectionName, con); return(true); }