/// <summary> /// 根据config获得MongoClientSettings,同时更新一些运行时变量 /// </summary> /// <param name="config"></param> /// <returns></returns> public static MongoClientSettings CreateMongoClientSettingsByConfig(ref MongoConnectionConfig config) { //修改获得数据实例的方法 var mongoClientSetting = new MongoClientSettings(); if (string.IsNullOrEmpty(config.ConnectionString)) { mongoClientSetting.ConnectionMode = ConnectionMode.Direct; ReadWrite.SetReadPreferenceWriteConcern(mongoClientSetting, config); //Replset时候可以不用设置吗? mongoClientSetting.Server = new MongoServerAddress(config.Host, config.Port); //MapReduce的时候将消耗大量时间。不过这里需要平衡一下,太长容易造成并发问题 //The default value for SocketTimeout has been changed from 30 seconds to 0, if (config.SocketTimeoutMs != 0) { mongoClientSetting.SocketTimeout = new TimeSpan(0, 0, (int)(config.SocketTimeoutMs / 1000)); } if (config.ConnectTimeoutMs != 0) { mongoClientSetting.ConnectTimeout = new TimeSpan(0, 0, (int)(config.ConnectTimeoutMs / 1000)); } //运行时LoginAsAdmin的设定 config.LoginAsAdmin = config.DataBaseName == string.Empty; if (config.InputPasswordOnConnect && config.Password == string.Empty && GetPassword != null) { config.Password = GetPassword(config.UserName); } if (!(string.IsNullOrEmpty(config.UserName) || string.IsNullOrEmpty(config.Password))) { //认证的设定:注意,这里的密码是明文 //3.0开始默认SCRAM-SHA-1 if (config.AuthMechanism == ConstMgr.MONGODB_CR) { if (string.IsNullOrEmpty(config.DataBaseName)) { mongoClientSetting.Credentials = new[] { MongoCredential.CreateMongoCRCredential(ConstMgr.DatabaseNameAdmin, config.UserName, config.Password) }; } else { mongoClientSetting.Credentials = new[] { MongoCredential.CreateMongoCRCredential(config.DataBaseName, config.UserName, config.Password) }; } } if (config.AuthMechanism == ConstMgr.SCRAM_SHA_1) { if (string.IsNullOrEmpty(config.DataBaseName)) { mongoClientSetting.Credentials = new[] { MongoCredential.CreateCredential(ConstMgr.DatabaseNameAdmin, config.UserName, config.Password) }; } else { mongoClientSetting.Credentials = new[] { MongoCredential.CreateCredential(config.DataBaseName, config.UserName, config.Password) }; } } } //X509 if (!string.IsNullOrEmpty(config.UserName)) { if (config.AuthMechanism == ConstMgr.MONGODB_X509) { mongoClientSetting.Credentials = new[] { MongoCredential.CreateMongoX509Credential(config.UserName) }; } } //ReplSetName if (!string.IsNullOrEmpty(config.ReplSetName)) { mongoClientSetting.ReplicaSetName = config.ReplSetName; config.ServerRole = MongoConnectionConfig.SvrRoleType.ReplsetSvr; } else { config.ServerRole = MongoConnectionConfig.SvrRoleType.DataSvr; } if (config.ServerRole == MongoConnectionConfig.SvrRoleType.ReplsetSvr) { //ReplsetName不是固有属性,可以设置,不过必须保持与配置文件的一致 mongoClientSetting.ConnectionMode = ConnectionMode.ReplicaSet; //添加Replset服务器,注意,这里可能需要事先初始化副本 var replsetSvrList = new List <MongoServerAddress>(); foreach (var item in config.ReplsetList) { //如果这里的服务器在启动的时候没有--Replset参数,将会出错,当然作为单体的服务器,启动是没有任何问题的 MongoServerAddress replSrv; if (item.Split(":".ToCharArray()).Length == 2) { replSrv = new MongoServerAddress( item.Split(":".ToCharArray())[0], Convert.ToInt16(item.Split(":".ToCharArray())[1])); } else { replSrv = new MongoServerAddress(item); } replsetSvrList.Add(replSrv); } mongoClientSetting.Servers = replsetSvrList; } } else { //使用MongoConnectionString建立连接 mongoClientSetting = MongoClientSettings.FromUrl(MongoUrl.Create(config.ConnectionString)); } //为了避免出现无法读取数据库结构的问题,将读权限都设置为Preferred if (Equals(mongoClientSetting.ReadPreference, ReadPreference.Primary)) { mongoClientSetting.ReadPreference = ReadPreference.PrimaryPreferred; } if (Equals(mongoClientSetting.ReadPreference, ReadPreference.Secondary)) { mongoClientSetting.ReadPreference = ReadPreference.SecondaryPreferred; } return(mongoClientSetting); }
/// <summary> /// MongoDB存储基类 /// </summary> /// <param name="mongoUrl">mongo连接字符串</param> /// <param name="databaseName">mongo数据库名称</param> public MongoStorageBase(string mongoUrl, string databaseName) { this.Settings = MongoClientSettings.FromUrl(MongoUrl.Create(mongoUrl)); this.DatabaseName = databaseName; }
public MongoDbHealthCheck(string connectionString, string databaseName = default) : this(MongoClientSettings.FromUrl(MongoUrl.Create(connectionString)), databaseName) { }
public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context) { try { log.LogInformation("C# HTTP trigger function processed a request."); //get POST body string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); log.LogInformation($"data -> {data}"); //get environment variables /* * var config = new ConfigurationBuilder() * .SetBasePath(context.FunctionAppDirectory) * .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) * .AddEnvironmentVariables() * .Build(); */ //access azure keyvault var serviceTokenProvider = new AzureServiceTokenProvider(); log.LogInformation("serviceTokenProvider"); var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(serviceTokenProvider.KeyVaultTokenCallback)); log.LogInformation("keyVaultClient"); SecretBundle secretValue; try { secretValue = await keyVaultClient.GetSecretAsync("https://francesco-key-vault.vault.azure.net/secrets/mongodb/"); log.LogInformation("Secret Value retrieved from KeyVault."); } catch (Exception kex) { return((ObjectResult) new ObjectResult(kex.Message.ToString())); } //connect to MongoDB string connectionString = $@"{secretValue.Value}"; MongoClientSettings settings = MongoClientSettings.FromUrl( new MongoUrl(connectionString) ); log.LogInformation($"connection established"); //enable SSL settings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 }; var mongoClient = new MongoClient(settings); log.LogInformation($"SSL enabled"); //get db handle IMongoDatabase db = mongoClient.GetDatabase("outDatabase"); log.LogInformation($"DB handle aquired"); //create collection //await db.CreateCollectionAsync("Books"); //log.LogInformation($"Collection created"); //create document var document = new BsonDocument { { "id", BsonValue.Create(data["id"].ToString()) }, { "title", BsonValue.Create(data["title"].ToString()) }, { "description", BsonValue.Create(data["description"].ToString()) }, { "author", BsonValue.Create(data["author"].ToString()) } }; log.LogInformation($"document created"); //get collection handle we just created ?? var collection = db.GetCollection <BsonDocument>("Books"); //insert into db await collection.InsertOneAsync(document); log.LogInformation($"document inserted -> {document}"); return((ActionResult) new OkObjectResult($"200ok, DB write successful -> , {data}")); } catch (Exception e) { return((ActionResult) new BadRequestObjectResult("400")); } }
protected virtual MongoClientSettings OnCreateClientSettings() { return(MongoClientSettings.FromUrl(new MongoUrl(databaseSettings.ConnectionString))); }
public TBMongoDatabase(string databaseName, string connectionString) { _settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString)); DatabaseName = databaseName; }
public PlainAuthenticationTests() { _settings = MongoClientSettings.FromUrl(new MongoUrl(CoreTestConfiguration.ConnectionString.ToString())); }
public void Setup() { _settings = MongoClientSettings.FromUrl(new MongoUrl(CoreTestConfiguration.ConnectionString.ToString())); }
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseNServiceBus(hostBuilderContext => { var endpointConfiguration = new EndpointConfiguration(EndpointName); endpointConfiguration.UseSerialization <SystemJsonSerializer>(); var transport = endpointConfiguration.UseTransport <RabbitMQTransport>(); transport.ConnectionString("host=localhost"); transport.UseConventionalRoutingTopology(); endpointConfiguration.UsePersistence <LearningPersistence>(); endpointConfiguration.EnableInstallers(); endpointConfiguration.AuditProcessedMessagesTo("NsbActivities.Audit"); endpointConfiguration.PurgeOnStartup(true); var recoverability = endpointConfiguration.Recoverability(); recoverability.Immediate(i => i.NumberOfRetries(1)); recoverability.Delayed(i => i.NumberOfRetries(0)); var settings = endpointConfiguration.GetSettings(); settings.Set(new NServiceBus.Extensions.Diagnostics.InstrumentationOptions { CaptureMessageBody = true }); // configure endpoint here return(endpointConfiguration); }) .ConfigureServices(services => { var runner = MongoDbRunner.Start(singleNodeReplSet: true, singleNodeReplSetWaitTimeout: 20); services.AddSingleton(runner); var urlBuilder = new MongoUrlBuilder(runner.ConnectionString) { DatabaseName = "dev" }; var mongoUrl = urlBuilder.ToMongoUrl(); var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl); mongoClientSettings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber(new InstrumentationOptions { CaptureCommandText = true })); var mongoClient = new MongoClient(mongoClientSettings); services.AddSingleton(mongoUrl); services.AddSingleton(mongoClient); services.AddTransient(provider => provider.GetService <MongoClient>().GetDatabase(provider.GetService <MongoUrl>().DatabaseName)); services.AddHostedService <Mongo2GoService>(); services.AddOpenTelemetryTracing(builder => builder .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(EndpointName)) .AddAspNetCoreInstrumentation() .AddMongoDBInstrumentation() .AddNServiceBusInstrumentation() .AddZipkinExporter(o => { o.Endpoint = new Uri("http://localhost:9411/api/v2/spans"); }) .AddJaegerExporter(c => { c.AgentHost = "localhost"; c.AgentPort = 6831; })); }) ;
public void TestFromUrl() { // set everything to non default values to test that all settings are converted var connectionString = "mongodb://*****:*****@somehost/?appname=app1;authSource=db;authMechanismProperties=CANONICALIZE_HOST_NAME:true;" + "compressors=zlib,snappy;zlibCompressionLevel=9;connect=direct;connectTimeout=123;ipv6=true;heartbeatInterval=1m;heartbeatTimeout=2m;localThreshold=128;" + "maxIdleTime=124;maxLifeTime=125;maxPoolSize=126;minPoolSize=127;readConcernLevel=majority;" + "readPreference=secondary;readPreferenceTags=a:1,b:2;readPreferenceTags=c:3,d:4;retryReads=false;retryWrites=true;socketTimeout=129;" + "serverSelectionTimeout=20s;tls=true;tlsInsecure=true;waitqueuesize=130;waitQueueTimeout=131;" + "w=1;fsync=true;journal=true;w=2;wtimeout=131;gssapiServiceName=other"; #pragma warning disable 618 if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2) { connectionString += ";uuidRepresentation=pythonLegacy"; } #pragma warning restore 618 var builder = new MongoUrlBuilder(connectionString); var url = builder.ToMongoUrl(); var settings = MongoClientSettings.FromUrl(url); Assert.Equal(url.AllowInsecureTls, settings.AllowInsecureTls); Assert.Equal(url.ApplicationName, settings.ApplicationName); Assert.Equal(url.Compressors, settings.Compressors); Assert.Equal(url.ConnectionMode, settings.ConnectionMode); Assert.Equal(url.ConnectTimeout, settings.ConnectTimeout); #pragma warning disable 618 Assert.Equal(1, settings.Credentials.Count()); #pragma warning restore Assert.Equal(url.Username, settings.Credential.Username); Assert.Equal(url.AuthenticationMechanism, settings.Credential.Mechanism); Assert.Equal("other", settings.Credential.GetMechanismProperty <string>("SERVICE_NAME", "mongodb")); Assert.Equal(true, settings.Credential.GetMechanismProperty <bool>("CANONICALIZE_HOST_NAME", false)); Assert.Equal(url.AuthenticationSource, settings.Credential.Source); Assert.Equal(new PasswordEvidence(url.Password), settings.Credential.Evidence); #pragma warning disable 618 if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2) { Assert.Equal(url.GuidRepresentation, settings.GuidRepresentation); } #pragma warning restore 618 Assert.Equal(url.HeartbeatInterval, settings.HeartbeatInterval); Assert.Equal(url.HeartbeatTimeout, settings.HeartbeatTimeout); Assert.Equal(url.IPv6, settings.IPv6); Assert.Equal(url.LocalThreshold, settings.LocalThreshold); Assert.Equal(url.MaxConnectionIdleTime, settings.MaxConnectionIdleTime); Assert.Equal(url.MaxConnectionLifeTime, settings.MaxConnectionLifeTime); Assert.Equal(url.MaxConnectionPoolSize, settings.MaxConnectionPoolSize); Assert.Equal(url.MinConnectionPoolSize, settings.MinConnectionPoolSize); Assert.Equal(url.ReadConcernLevel, settings.ReadConcern.Level); Assert.Equal(url.ReadPreference, settings.ReadPreference); Assert.Equal(url.ReplicaSetName, settings.ReplicaSetName); Assert.Equal(url.RetryReads, settings.RetryReads); Assert.Equal(url.RetryWrites, settings.RetryWrites); Assert.Equal(url.Scheme, settings.Scheme); Assert.True(url.Servers.SequenceEqual(settings.Servers)); Assert.Equal(url.ServerSelectionTimeout, settings.ServerSelectionTimeout); Assert.Equal(url.SocketTimeout, settings.SocketTimeout); #pragma warning disable 618 settings.SslSettings.Should().BeNull(); Assert.Equal(url.UseSsl, settings.UseSsl); #pragma warning restore 618 Assert.Equal(url.UseTls, settings.UseTls); #pragma warning disable 618 Assert.Equal(url.VerifySslCertificate, settings.VerifySslCertificate); #pragma warning restore 618 #pragma warning disable 618 Assert.Equal(url.ComputedWaitQueueSize, settings.WaitQueueSize); #pragma warning restore 618 Assert.Equal(url.WaitQueueTimeout, settings.WaitQueueTimeout); Assert.Equal(url.GetWriteConcern(true), settings.WriteConcern); }
/// <summary> /// Initializes a new instance of the MongoClient class. /// </summary> /// <param name="url">The URL.</param> public MongoClient(MongoUrl url) : this(MongoClientSettings.FromUrl(url)) { }