示例#1
0
        public MongoRepositoryBase(MongoDBConnectionWraper connection)
        {
            _connection = connection;

            var db = connection.MongoClient.GetDatabase(connection.MongoURL.DatabaseName);

            _collection = db.GetCollection <T>(GetCollectionName());
        }
示例#2
0
        public static IServiceCollection AddMongoDB(this IServiceCollection services, IConfiguration configuration)
        {
            var mongoUri = configuration.GetConnectionString("MongoDB");

            // MongoClient (Singleton)
            var mongoUrl        = new MongoUrl(mongoUri);
            var mongoConnection = new MongoDBConnectionWraper
            {
                MongoURL    = mongoUrl,
                MongoClient = new MongoClient(mongoUrl)
            };

            services.AddSingleton(mongoConnection);

            services.AddHealthChecks()
            .AddMongoDb(mongoUri, "mongodb", failureStatus: null, tags: new string[] { "db", "mongodb" });

            return(services);
        }
示例#3
0
        public static IServiceCollection AddMongoDB(this IServiceCollection services)
        {
            var mongoUri = CommonHelpers.GetValueFromEnv <string>("MONGO_URI");

            // MongoClient (Singleton)
            var mongoUrl        = new MongoUrl(mongoUri);
            var mongoConnection = new MongoDBConnectionWraper
            {
                MongoURL    = mongoUrl,
                MongoClient = new MongoClient(mongoUrl)
            };

            services.AddSingleton(mongoConnection);

            services.AddHealthChecks()
            .AddMongoDb(mongoUri, "mongodb", failureStatus: null, tags: new string[] { "db", "mongodb" });

            return(services);
        }
示例#4
0
        public static IServiceCollection AddMongoDB(this IServiceCollection services, IConfiguration configuration)
        {
            services.Configure <MongoDBSettings>(configuration.GetSection(nameof(MongoDBSettings)));

            var settings = services.BuildServiceProvider().GetRequiredService <IOptions <MongoDBSettings> >().Value;

            // MongoClient (Singleton)
            var mongoUrl        = new MongoUrl(settings.Uri);
            var mongoConnection = new MongoDBConnectionWraper
            {
                MongoURL    = mongoUrl,
                MongoClient = new MongoClient(mongoUrl)
            };

            services.AddSingleton(mongoConnection);

            services.AddHealthChecks()
            .AddMongoDb(settings.Uri, "mongodb", failureStatus: null, tags: new string[] { "db", "mongodb" });

            return(services);
        }