Browse Source

Merge pull request #9774 from abpframework/liangshiwei/mongodb

Make MongoClientSettings configurable
pull/9782/head
maliming 5 years ago
committed by GitHub
parent
commit
144d9d4e65
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContextOptions.cs
  2. 12
      framework/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDbContextProvider.cs

4
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContextOptions.cs

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Driver;
namespace Volo.Abp.MongoDB namespace Volo.Abp.MongoDB
{ {
@ -7,6 +9,8 @@ namespace Volo.Abp.MongoDB
{ {
internal Dictionary<Type, Type> DbContextReplacements { get; } internal Dictionary<Type, Type> DbContextReplacements { get; }
public Action<MongoClientSettings> MongoClientSettingsConfigurer { get; set; }
public AbpMongoDbContextOptions() public AbpMongoDbContextOptions()
{ {
DbContextReplacements = new Dictionary<Type, Type>(); DbContextReplacements = new Dictionary<Type, Type>();

12
framework/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDbContextProvider.cs

@ -124,7 +124,7 @@ namespace Volo.Abp.Uow.MongoDB
private TMongoDbContext CreateDbContext(IUnitOfWork unitOfWork, MongoUrl mongoUrl, string databaseName) private TMongoDbContext CreateDbContext(IUnitOfWork unitOfWork, MongoUrl mongoUrl, string databaseName)
{ {
var client = new MongoClient(mongoUrl); var client = CreateMongoClient(mongoUrl);
var database = client.GetDatabase(databaseName); var database = client.GetDatabase(databaseName);
if (unitOfWork.Options.IsTransactional) if (unitOfWork.Options.IsTransactional)
@ -144,7 +144,7 @@ namespace Volo.Abp.Uow.MongoDB
string databaseName, string databaseName,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var client = new MongoClient(mongoUrl); var client = CreateMongoClient(mongoUrl);
var database = client.GetDatabase(databaseName); var database = client.GetDatabase(databaseName);
if (unitOfWork.Options.IsTransactional) if (unitOfWork.Options.IsTransactional)
@ -273,6 +273,14 @@ namespace Volo.Abp.Uow.MongoDB
return _connectionStringResolver.Resolve(dbContextType); return _connectionStringResolver.Resolve(dbContextType);
} }
private MongoClient CreateMongoClient(MongoUrl mongoUrl)
{
var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl);
_options.MongoClientSettingsConfigurer?.Invoke(mongoClientSettings);
return new MongoClient(mongoClientSettings);
}
protected virtual CancellationToken GetCancellationToken(CancellationToken preferredValue = default) protected virtual CancellationToken GetCancellationToken(CancellationToken preferredValue = default)
{ {
return _cancellationTokenProvider.FallbackToProvider(preferredValue); return _cancellationTokenProvider.FallbackToProvider(preferredValue);

Loading…
Cancel
Save