示例#1
0
        public void TestFreeze()
        {
            var settings = new MongoClientSettings();

            Assert.False(settings.IsFrozen);
            var hashCode             = settings.GetHashCode();
            var stringRepresentation = settings.ToString();

            settings.Freeze();
            Assert.True(settings.IsFrozen);
            Assert.Equal(hashCode, settings.GetHashCode());
            Assert.Equal(stringRepresentation, settings.ToString());
        }
        public async Task <HealthCheckResult> CheckHealthAsync(CancellationToken cancellationToken = default)
        {
            try
            {
                var mongoClient = _mongoClient.GetOrAdd(_mongoClientSettings.ToString(), _ => new MongoClient(_mongoClientSettings));

                if (!string.IsNullOrWhiteSpace(_dataBaseName))
                {
                    var cursor = await mongoClient.GetDatabase(_dataBaseName).ListCollectionNamesAsync(cancellationToken: cancellationToken);

                    await cursor.FirstOrDefaultAsync(cancellationToken);

                    cursor.Dispose();
                }
                else
                {
                    var cursor = await mongoClient.ListDatabaseNamesAsync(cancellationToken);

                    await cursor.FirstOrDefaultAsync(cancellationToken);

                    cursor.Dispose();
                }

                return(new HealthCheckResult(HealthStatus.Healthy, "MongoDb is healthy"));
            }
            catch (Exception ex)
            {
                return(new HealthCheckResult(HealthStatus.Unhealthy, "MongoDb is unhealthy", exception: ex));
            }
        }
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            try
            {
                var mongoClient = _mongoClient.GetOrAdd(_mongoClientSettings.ToString(), _ => new MongoClient(_mongoClientSettings));

                if (!string.IsNullOrEmpty(_specifiedDatabase))
                {
                    // some users can't list all databases depending on database privileges, with
                    // this you can list only collections on specified database.
                    // Related with issue #43

                    using var cursor = await mongoClient
                                       .GetDatabase(_specifiedDatabase)
                                       .ListCollectionNamesAsync(cancellationToken: cancellationToken);

                    await cursor.FirstAsync(cancellationToken);
                }
                else
                {
                    using var cursor = await mongoClient.ListDatabaseNamesAsync(cancellationToken);

                    await cursor.FirstOrDefaultAsync(cancellationToken);
                }

                return(HealthCheckResult.Healthy());
            }
            catch (Exception ex)
            {
                return(new HealthCheckResult(context.Registration.FailureStatus, exception: ex));
            }
        }
        public void TestFreeze()
        {
            var settings = new MongoClientSettings();

            settings.ReadPreference = new ReadPreference();
            settings.WriteConcern   = new WriteConcern();

            Assert.IsFalse(settings.IsFrozen);
            Assert.IsFalse(settings.ReadPreference.IsFrozen);
            Assert.IsFalse(settings.WriteConcern.IsFrozen);
            var hashCode             = settings.GetHashCode();
            var stringRepresentation = settings.ToString();

            settings.Freeze();
            Assert.IsTrue(settings.IsFrozen);
            Assert.IsTrue(settings.ReadPreference.IsFrozen);
            Assert.IsTrue(settings.WriteConcern.IsFrozen);
            Assert.AreEqual(hashCode, settings.GetHashCode());
            Assert.AreEqual(stringRepresentation, settings.ToString());
        }
        public void TestFreeze()
        {
            var settings = new MongoClientSettings();

            Assert.IsFalse(settings.IsFrozen);
            var hashCode = settings.GetHashCode();
            var stringRepresentation = settings.ToString();

            settings.Freeze();
            Assert.IsTrue(settings.IsFrozen);
            Assert.AreEqual(hashCode, settings.GetHashCode());
            Assert.AreEqual(stringRepresentation, settings.ToString());
        }