private void LoadClientMapFromXmlFile() { var xmlDocument = XDocument.Parse(File.ReadAllText(this.ConfigFilePath)); var rootNode = xmlDocument.Root; if (rootNode == null) { throw new XmlException("Load configuration file root node failed."); } if (rootNode.Name.LocalName.EqualsIgnoreCase("mongoDBConfiguration")) { foreach (var mongodbNode in rootNode.Elements()) { if (mongodbNode.Name.LocalName.EqualsIgnoreCase("mongoDB")) { foreach (var mongoClientNode in mongodbNode.Elements()) { if (mongoClientNode.Name.LocalName.EqualsIgnoreCase("mongoClient")) { var idAttribute = mongoClientNode.Attribute("id"); var instanceId = Guid.NewGuid();; if (idAttribute != null && !string.IsNullOrEmpty(idAttribute.Value)) { instanceId = new Guid(idAttribute.Value); } var connectionString = mongoClientNode.Attribute("connectionString").Value; var mongoSettings = MongoClientSettings.FromUrl(new MongoUrl(connectionString)); MongoDBClientPool.RegisterMongoDBClient(instanceId, mongoSettings); var modeAttribute = mongoClientNode.Attribute("mode"); if (modeAttribute == null) { // If not specify the MongoDB mode type it will NOT be considered as full mode. continue; } switch (modeAttribute.Value.ToLower()) { case "read": this.ReadableClientMap.Add(instanceId); break; case "write": this.WritableClientMap.Add(instanceId); break; case "full": this.ReadableClientMap.Add(instanceId); this.WritableClientMap.Add(instanceId); break; } } } } } } }
public virtual MongoClient GetPriorityWritableClient() { this.SyncClientStatus(); if (!this.WritableClientMap.Any()) { throw new MongoException("There without any available MongoDB client for write."); } var writableClientId = this.WritableClientMap.First(); return MongoDBClientPool.GetMongoDBClient(writableClientId); }
public MongoClient GetDefaultClient() { return(MongoDBClientPool.GetDefaultMongoDBClient()); }
public IClientHandler ReloadClientMap() { MongoDBClientPool.CleanMongoDBClientPool(); this.ClearClientMap(); return(this.LoadClientMap()); }