本文整理汇总了C#中MongoDB.Driver.MongoClientSettings类的典型用法代码示例。如果您正苦于以下问题:C# MongoClientSettings类的具体用法?C# MongoClientSettings怎么用?C# MongoClientSettings使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MongoClientSettings类属于MongoDB.Driver命名空间,在下文中一共展示了MongoClientSettings类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSettings
MongoClientSettings GetSettings()
{
if (String.IsNullOrEmpty(MongoSettings.ConnectionString))
{
throw new ArgumentNullException("Connection string not found.");
}
if (String.IsNullOrEmpty(MongoSettings.Database))
{
throw new ArgumentNullException("Database string not found.");
}
var ips = MongoSettings.ConnectionString.Split(';');
var servicesList = new List<MongoServerAddress>();
foreach (var ip in ips)
{
var host = ip.Split(':')[0];
var port = Convert.ToInt32(ip.Split(':')[1]);
servicesList.Add(new MongoServerAddress(host, port));
}
var setting = new MongoClientSettings();
setting.ReplicaSetName = MongoSettings.ReplicaSetName;
//集群中的服务器列表
setting.Servers = servicesList;
return setting;
}
示例2: GetOrCreateCluster_should_return_a_cluster_with_the_correct_settings
public void GetOrCreateCluster_should_return_a_cluster_with_the_correct_settings()
{
var credentials = new[] { MongoCredential.CreateMongoCRCredential("source", "username", "password") };
var servers = new[] { new MongoServerAddress("localhost"), new MongoServerAddress("127.0.0.1", 30000), new MongoServerAddress("[::1]", 27018) };
var sslSettings = new SslSettings
{
CheckCertificateRevocation = true,
EnabledSslProtocols = SslProtocols.Tls
};
var clientSettings = new MongoClientSettings
{
ApplicationName = "app1",
ConnectionMode = ConnectionMode.ReplicaSet,
ConnectTimeout = TimeSpan.FromSeconds(1),
Credentials = credentials,
GuidRepresentation = GuidRepresentation.Standard,
HeartbeatInterval = TimeSpan.FromSeconds(7),
HeartbeatTimeout = TimeSpan.FromSeconds(8),
IPv6 = true,
MaxConnectionIdleTime = TimeSpan.FromSeconds(2),
MaxConnectionLifeTime = TimeSpan.FromSeconds(3),
MaxConnectionPoolSize = 10,
MinConnectionPoolSize = 5,
ReplicaSetName = "rs",
LocalThreshold = TimeSpan.FromMilliseconds(20),
Servers = servers,
ServerSelectionTimeout = TimeSpan.FromSeconds(5),
SocketTimeout = TimeSpan.FromSeconds(4),
SslSettings = sslSettings,
UseSsl = true,
VerifySslCertificate = true,
WaitQueueSize = 20,
WaitQueueTimeout = TimeSpan.FromSeconds(6)
};
var subject = new ClusterRegistry();
using (var cluster = subject.GetOrCreateCluster(clientSettings.ToClusterKey()))
{
var endPoints = new EndPoint[]
{
new DnsEndPoint("localhost", 27017),
new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30000),
new IPEndPoint(IPAddress.Parse("[::1]"), 27018)
};
cluster.Settings.ConnectionMode.Should().Be(ClusterConnectionMode.ReplicaSet);
cluster.Settings.EndPoints.Equals(endPoints);
cluster.Settings.ReplicaSetName.Should().Be("rs");
cluster.Settings.ServerSelectionTimeout.Should().Be(clientSettings.ServerSelectionTimeout);
cluster.Settings.PostServerSelector.Should().NotBeNull().And.Subject.Should().BeOfType<LatencyLimitingServerSelector>();
cluster.Settings.MaxServerSelectionWaitQueueSize.Should().Be(20);
cluster.Description.Servers.Select(s => s.EndPoint).Should().Contain(endPoints);
// TODO: don't know how to test the rest of the settings because they are all private to the cluster
}
}
示例3: parse_from_connection_string
protected void parse_from_connection_string( string connection_string ) {
var mongo_url = MongoUrl.Create( connection_string );
ConnectionString = connection_string;
DatabaseName = mongo_url.DatabaseName;
MongoClientSettings = MongoClientSettings.FromUrl( mongo_url );
}
示例4: GetDatabase
public IMongoDatabase GetDatabase(string user, string password, string db = defaultDB) {
var credential = MongoCredential.CreateMongoCRCredential(db, user, password);
var settings = new MongoClientSettings { Credentials = new[] { credential } };
var mongoClient = new MongoClient(settings);
return GetDatabase(mongoClient, db);
}
示例5: ToMongoClientSettings
/// <summary>
/// Converts the current instance to a <see cref="MongoClientSettings"/>.
/// </summary>
/// <returns>The current instance as a <see cref="MongoClientSettings"/>.</returns>
public virtual MongoClientSettings ToMongoClientSettings()
{
MongoClientSettings settings;
if (this.Servers == null || !this.Servers.Any())
{
settings = new MongoClientSettings
{
Server = new MongoServerAddress(this.Host, this.Port)
};
}
else
{
// Support multiple servers if needed (e.g. replication)
settings = new MongoClientSettings
{
Servers = this.Servers.Select(s => new MongoServerAddress(s.Host, s.Port))
};
}
// Set replica set
settings.ReplicaSetName = this.ReplicaSet;
if (this.Authenticate)
{
settings.Credentials = new[] { MongoCredential.CreateMongoCRCredential(this.Database, this.Username, this.Password) };
}
return settings;
}
示例6: Connect
public override bool Connect() {
bool opened = true;
if (this.Client == null) {
try {
MongoClientSettings settings = new MongoClientSettings();
if (this.Settings.Hostname != null && this.Settings.Port.HasValue == true) {
settings.Server = new MongoServerAddress(this.Settings.Hostname, (int)this.Settings.Port.Value);
}
else if (this.Settings.Hostname != null) {
settings.Server = new MongoServerAddress(this.Settings.Hostname);
}
if (this.Database != null && this.Settings.Username != null && this.Settings.Password != null) {
settings.Credentials = new List<MongoCredential>() {
MongoCredential.CreateMongoCRCredential(this.Settings.Database, this.Settings.Username, this.Settings.Password)
};
}
this.Client = new MongoClient(settings);
this.Database = this.Client.GetServer().GetDatabase(this.Settings.Database);
}
catch {
opened = false;
}
}
return opened;
}
示例7: TestRealMongo
public ActionResult TestRealMongo()
{
//Connect to MongoDB in C# with Credentials
var credential = MongoCredential.CreateMongoCRCredential("alertsmd", "rangle", "m3anstack");
//Server settings
var settings = new MongoClientSettings
{
Credentials = new[] { credential },
Server = new MongoServerAddress("ds033757.mongolab.com",33757)
};
//Get a Reference to the Client Object
var mongoClient = new MongoClient(settings);
var mongoServer = mongoClient.GetServer();
var database = mongoServer.GetDatabase("alertsmd");
var collections = database.GetCollectionNames();
var usersCollection = database.GetCollection("users");
var mconditions = database.GetCollection("conditions").FindAll();
var mfeedbacks = database.GetCollection("feedbacks").FindAll();
var mprofiles = database.GetCollection("profiles").FindAll();
var msessions = database.GetCollection("sessions").FindAll();
var msymptoms = database.GetCollection("symptoms").FindAll();
var msystem_indexes = database.GetCollection("system.indexes").FindAll();
var msystem_users = database.GetCollection("system.users").FindAll();
var muserDatas = database.GetCollection("userDatas").FindAll();
var musers = database.GetCollection("users").FindAll();
return View();
}
示例8: LocalContext
public LocalContext()
{
MongoClientSettings settings = new MongoClientSettings();
settings.Server = new MongoServerAddress("localhost", 27017);
//settings.Server = new MongoServerAddress("192.168.0.103", 27017);
var client = new MongoClient(settings);
Database = client.GetDatabase("admin");
}
示例9: MongoDashboardContext
public MongoDashboardContext()
{
var settings = new MongoClientSettings();
settings.Server = new MongoServerAddress("127.0.0.1");
var client = new MongoClient(settings);
var database = client.GetDatabase("Workspaces");
Dashboards = database.GetCollection<Dashboard>("Dashboard");
}
示例10: MongoStore
public MongoStore(string db)
{
var clientSettings = new MongoClientSettings();
//clientSettings.Server = new MongoServerAddress("mongodb://host:22666");
var mongo = new MongoClient("mongodb://localhost:22666");
var l = mongo.ListDatabasesAsync().Result;
_mongodb = mongo.GetDatabase(db);
}
示例11: GetTestDatabase
protected static MongoDatabase GetTestDatabase()
{
var settings = new MongoClientSettings
{
Server = new MongoServerAddress("localhost")
};
return new MongoClient(settings).GetServer().GetDatabase("mongo-learning");
}
示例12: Create
public IMongoDatabase Create()
{
MongoClientSettings setting = new MongoClientSettings();
setting.Server = new MongoServerAddress(_serverAddress, 27017);
var mongoClient = new MongoClient(setting);
var db = mongoClient.GetDatabase(_databaseName);
return db;
}
示例13: MongoOptimisticDataStore
public MongoOptimisticDataStore(MongoClientSettings settings, string databaseName, string collectionName)
{
MongoClient client = new MongoClient(settings);
this.Server = client.GetServer();
this.CollectionName = collectionName;
this.Database = this.Server.GetDatabase(databaseName);
SetCollection();
this.IsConnected = true;
}
示例14: BookController
public BookController()
{
_repo = new Repository();
string connectionString = "mongodb://localhost:27017/";
var mongoClientSettings = new MongoClientSettings();
mongoClientSettings.Server = new MongoServerAddress(connectionString);
var client = new MongoClient();
var db = client.GetDatabase("BookMVC");
var collection = db.GetCollection<Book>("Book");
}
示例15: UseMongoStorage
public static MongoStorage UseMongoStorage(this IBootstrapperConfiguration configuration,
MongoClientSettings mongoClientSettings,
string databaseName)
{
MongoStorage storage = new MongoStorage(mongoClientSettings, databaseName);
configuration.UseStorage(storage);
return storage;
}