-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongoDbExtensions.cs
32 lines (24 loc) · 987 Bytes
/
MongoDbExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using Elastic.Apm.MongoDb;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Driver;
namespace ElasticAPMMongoDBDependency;
public static class MongoDbExtensions
{
private const string ConventionPackName = "pack";
public static IServiceCollection AddMongoDb(this IServiceCollection services, IConfiguration configuration)
{
var pack = new ConventionPack
{
new CamelCaseElementNameConvention()
};
ConventionRegistry.Register(ConventionPackName, pack, _ => true);
services.AddSingleton(provider =>
{
var mongoSettings = MongoClientSettings.FromConnectionString("connectionString");
mongoSettings.ServerApi = new ServerApi(ServerApiVersion.V1);
mongoSettings.ClusterConfigurator = builder => builder.Subscribe(new MongoDbEventSubscriber());
return new MongoClient(mongoSettings);
});
return services;
}
}