public void CanReadWithNonDefaultSectionName()
        {
            var section = MongoConfigurationSection.GetSection("mongoNonDefaultName");

            Assert.IsNotNull(section);
            Assert.AreEqual("Server=localhost:27018", section.Connections["local21018"].ConnectionString);
        }
        public void CanCreateConfigurationFromSection()
        {
            var section = MongoConfigurationSection.GetSection();
            var config  = section.CreateConfiguration();

            Assert.IsNotNull(config);
            Assert.AreEqual("Server=localhost:27017", config.ConnectionString);
        }
        public void CanReadFromTestsConfig()
        {
            var section = MongoConfigurationSection.GetSection();

            Assert.IsNotNull(section);
            Assert.AreEqual("Server=localhost:27017", section.Connections["default"].ConnectionString);
            Assert.AreEqual("Server=localhost:27018", section.Connections["local21018"].ConnectionString);
        }
示例#4
0
        /// <summary>
        /// 根据名字获取mongobd连接的实例
        ///
        ///public class MyClass
        ///{
        ///    [MongoId]
        ///    public string MyID { get; set; }
        ///    public string Name { get; set; }
        ///    public int Corners { get; set; }
        ///}
        ///
        ///    var db = MongoDBHelper.GetInstance();
        ///    db.Excute("mytestqqq", process =>
        ///        {
        ///            var c = process.GetCollection<MyClass>();
        ///            var mc = new MyClass()
        ///            {
        ///                myID = "CustomID",
        ///                Name = "BarfooUser",
        ///                Corners = 1
        ///            };
        ///
        ///            c.Save(mc);
        ///            var cc = c.FindAll();
        ///
        ///            foreach (var item in cc.Documents)
        ///            {
        ///                Console.WriteLine(item.myID);
        ///                Console.WriteLine(item.Name);
        ///                Console.WriteLine(item.Corners);
        ///            }
        ///        });
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static MongoDBHelper GetInstance(string name)
        {
            var connectionString = string.Empty;

            if (instances.TryGetValue(name, out connectionString))
            {
                return(new MongoDBHelper(connectionString));
            }
            else
            {
                var section = MongoConfigurationSection.GetSection();

                if (section != null &&
                    section.Connections[name] != null &&
                    !string.IsNullOrEmpty(section.Connections[name].ConnectionString))
                {
                    connectionString = section.Connections[name].ConnectionString;
                    instances[name]  = connectionString;
                    return(new MongoDBHelper(connectionString));
                }
                throw new ConfigurationErrorsException("Unable to find MongoDB instance \"" + name + "\".");
            }
        }