/// <summary> /// MognoDb配置 /// </summary> /// <param name="doc"></param> /// <param name="mongodb"></param> /// <returns></returns> public static XmlDocument SetMongoDbConfig(XmlDocument doc, MongoDbConfig mongodb) { doc = GetXmlDocument(doc, "configuration"); if (mongodb == null) return doc; var mongoElement = doc.CreateElement("mongodb"); var serverElement = GetMongoDbElement(doc, mongodb); mongoElement.AppendChild(serverElement); doc.AppendChild(mongoElement); return doc; }
/// <summary> /// 获取MongoDb在配置文件中的节点 /// </summary> /// <param name="doc"></param> /// <param name="mongodb"></param> /// <returns></returns> private static XmlElement GetMongoDbElement(XmlDocument doc, MongoDbConfig mongodb) { if (mongodb.MaxConnectionPoolSize == default(int)) mongodb.MaxConnectionPoolSize = 50; if (mongodb.MinConnectionPoolSize == default(int)) mongodb.MinConnectionPoolSize = 10; if (mongodb.WaitQueueSize == default(int)) mongodb.WaitQueueSize = 10000; if (mongodb.ConnectTimeout == default(TimeSpan)) mongodb.ConnectTimeout = new TimeSpan(0, 0, 30); if (mongodb.MaxConnectionIdleTime == default(TimeSpan)) mongodb.MaxConnectionIdleTime = new TimeSpan(0, 10, 0); if (mongodb.MaxConnectionLifeTime == default(TimeSpan)) mongodb.MaxConnectionLifeTime = new TimeSpan(0, 30, 0); if (mongodb.SocketTimeout == default(TimeSpan)) mongodb.SocketTimeout = new TimeSpan(0, 0, 0); if (mongodb.WaitQueueTimeout == default(TimeSpan)) mongodb.WaitQueueTimeout = new TimeSpan(0, 2, 0); var serverElement = doc.CreateElement("server"); serverElement.SetAttribute("name", mongodb.Name); serverElement.SetAttribute("username", mongodb.UserName); serverElement.SetAttribute("password", mongodb.PassWord); if (mongodb.Uris != null && mongodb.Uris.Count > 0) { foreach (var uri in mongodb.Uris) { var addElement = doc.CreateElement("add"); addElement.SetAttribute("uri", uri.ToString()); serverElement.AppendChild(addElement); } } var socketPoolElement = doc.CreateElement("socketPool"); socketPoolElement.SetAttribute("maxConnectionPoolSize", mongodb.MaxConnectionPoolSize.ToString()); socketPoolElement.SetAttribute("minConnectionPoolSize", mongodb.MinConnectionPoolSize.ToString()); socketPoolElement.SetAttribute("waitQueueSize", mongodb.WaitQueueSize.ToString()); socketPoolElement.SetAttribute("connectTimeout", mongodb.ConnectTimeout.ToString()); socketPoolElement.SetAttribute("maxConnectionIdleTime", mongodb.MaxConnectionIdleTime.ToString()); socketPoolElement.SetAttribute("maxConnectionLifeTime", mongodb.MaxConnectionLifeTime.ToString()); socketPoolElement.SetAttribute("socketTimeout", mongodb.SocketTimeout.ToString()); socketPoolElement.SetAttribute("waitQueueTimeout", mongodb.WaitQueueTimeout.ToString()); serverElement.AppendChild(socketPoolElement); return serverElement; }
/// <summary> /// 读取MognoDb配置 /// </summary> /// <param name="zone"></param> /// <returns></returns> public static List<MongoDbConfig> GetMongoDbConfig(int zone) { var result = new List<MongoDbConfig>(); var path = GetEndpointPath(zone); var doc = XmlHelper.GetXmlDocCache(path); var nodes = doc.SelectNodes("/configuration/mongodb/server"); if (nodes == null || nodes.Count <= 0) return result; foreach (XmlNode node in nodes) { var mongoDb = new MongoDbConfig { Name = XmlHelper.GetXmlAttributeValue(node, "name", string.Empty), UserName = XmlHelper.GetXmlAttributeValue(node, "username", string.Empty), PassWord = XmlHelper.GetXmlAttributeValue(node, "password", string.Empty), Uris = new List<Uri>() }; foreach (XmlNode item in node.ChildNodes) { switch (item.Name) { case "add": var uri = new Uri(XmlHelper.GetXmlAttributeValue(item, "uri", "mongodb://127.0.0.1:27017")); mongoDb.Uris.Add(uri); break; case "socketPool": mongoDb.MaxConnectionPoolSize = XmlHelper.GetXmlAttributeValue(item, "maxConnectionPoolSize", 50); mongoDb.MinConnectionPoolSize = XmlHelper.GetXmlAttributeValue(item, "minConnectionPoolSize", 10); mongoDb.WaitQueueSize = XmlHelper.GetXmlAttributeValue(item, "waitQueueSize", 10000); mongoDb.ConnectTimeout = TimeSpan.Parse(XmlHelper.GetXmlAttributeValue(item, "connectTimeout", "00:00:30")); mongoDb.MaxConnectionIdleTime = TimeSpan.Parse(XmlHelper.GetXmlAttributeValue(item, "maxConnectionIdleTime", "00:10:00")); mongoDb.MaxConnectionLifeTime = TimeSpan.Parse(XmlHelper.GetXmlAttributeValue(item, "maxConnectionLifeTime", "00:30:00")); mongoDb.SocketTimeout = TimeSpan.Parse(XmlHelper.GetXmlAttributeValue(item, "socketTimeout", "00:00:00")); mongoDb.WaitQueueTimeout = TimeSpan.Parse(XmlHelper.GetXmlAttributeValue(item, "waitQueueTimeout", "00:02:00")); break; } } result.Add(mongoDb); } return result; }