public static void AddMyMongoDb(this IServiceCollection services, IConfiguration config) { InstantSerializer.Register(); LocalDateSerializer.Register(); LocalTimeSerializer.Register(); ConventionRegistry.Register("EnumStringConvention", new ConventionPack { new EnumRepresentationConvention(BsonType.String) }, t => true); services.Configure <MongoDbOptions>( config.GetSection("mongoDb")); services.AddSingletonAs(c => { var connectionString = c.GetRequiredService <IOptions <MongoDbOptions> >().Value.ConnectionString; return(new MongoClient(connectionString)); }) .As <IMongoClient>(); services.AddSingletonAs(c => { var databaseName = c.GetRequiredService <IOptions <MongoDbOptions> >().Value.DatabaseName; return(c.GetRequiredService <IMongoClient>().GetDatabase(databaseName)); }) .As <IMongoDatabase>(); }
public static void AddMyMongoDb(this IServiceCollection services, IConfiguration config) { ActivityContextSerializer.Register(); ActivitySpanIdSerializer.Register(); ActivityTraceIdSerializer.Register(); DurationSerializer.Register(); InstantSerializer.Register(); LocalDateSerializer.Register(); LocalTimeSerializer.Register(); ConventionRegistry.Register("EnumStringConvention", new ConventionPack { new EnumRepresentationConvention(BsonType.String) }, t => true); ConventionRegistry.Register("IgnoreExtraElements", new ConventionPack { new IgnoreExtraElementsConvention(true) }, t => true); services.ConfigureAndValidate <MongoDbOptions>(config, "storage:mongoDb"); services.AddSingletonAs(c => { var connectionString = c.GetRequiredService <IOptions <MongoDbOptions> >().Value.ConnectionString; return(new MongoClient(connectionString)); }) .As <IMongoClient>(); services.AddSingletonAs(c => { var databaseName = c.GetRequiredService <IOptions <MongoDbOptions> >().Value.DatabaseName; return(c.GetRequiredService <IMongoClient>().GetDatabase(databaseName)); }) .As <IMongoDatabase>(); }