public static void AddMongoDb(this IServiceCollection services, IConfiguration configuration) { MongoMappings.RegisterMappings(); var mongoConnectionString = configuration.GetConnectionString("MongoDb"); var mongoUrl = new MongoUrl(mongoConnectionString); services.AddSingleton(_ => new MongoClient(mongoUrl)); services.AddScoped(provider => { var client = provider.GetRequiredService <MongoClient>(); return(client.GetDatabase(mongoUrl.DatabaseName)); }); services.AddTransient <IGroupQueries, MongoGroupQueries>(); }
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { services.AddOptions(); services.Configure <MongoDBSettings>(hostContext.Configuration.GetSection(nameof(MongoDBSettings))); services.AddScoped <IMyService, MyServiceFirstApproach>(); services.AddScoped <IScopedProcessingService, ScopedProcessingService>(); services.AddScoped <IProductRepository, ProductRepositoryMongoDBFirstApproach>(); services.AddScoped(typeof(IGenericRepository <>), typeof(GenericRepositoryMongoDBFirstApproach <>)); MongoMappings.Configure(); services.AddScoped <IMongoDbContextFirstApproach, MongoDbContextFirstApproach>(); services.AddHostedService <ScopedWorker>(); });