Skip to content

Commit

Permalink
fix(auth-service): ping to establish db connection
Browse files Browse the repository at this point in the history
  • Loading branch information
codingben committed May 6, 2023
1 parent 0ab6dce commit af31304
Showing 1 changed file with 21 additions and 2 deletions.
@@ -1,4 +1,6 @@
using MongoDB.Driver;
using System;
using MongoDB.Driver;
using MongoDB.Bson;

namespace Authenticator.Infrastructure.MongoRepository
{
Expand All @@ -13,8 +15,25 @@ public MongoDatabaseProvider(string url)
{
var mongoUrl = new MongoUrl(url);
var mongoClient = new MongoClient(mongoUrl);

database = mongoClient.GetDatabase(mongoUrl.DatabaseName);

Ping(url);
}

/// <summary>
/// Force the connection to be established.
/// </summary>
private void Ping(string url)
{
try
{
var pingCommand = new BsonDocument("ping", 1);
database.RunCommand<BsonDocument>(pingCommand);
}
catch (Exception exception)
{
throw new Exception($"Failed to connect to MongoDB server at {url}. {exception.Message}", exception);
}
}

public IMongoDatabase Provide()
Expand Down

0 comments on commit af31304

Please sign in to comment.