Unable to create a MongoDb Client in Production Environment

Hi there,

I’ve created a MongoDB collection where I’m adding a document in my function using AddOneAsync() method. Which works absolutely fine when I’m debugging my program. However, when I try to run my program outside the debug environment, it strangely always fails to create a new MongoDb Client for some reason.

public async Task LogData(Usage usage)
{
// Write to MongoDb
try
{
// Get MongoDb configs
var mongoDbConfigs = MongoDbConnector.Instance;
mongoDbConfigs.GetMongoDbConnectorDetailsFromJson();

   // Create a new MongoDb client
    var mongoClient = new MongoClient(mongoDbConfigs.MongoDbConnectionString); // This line fails.
    var db = mongoClient.GetDatabase(mongoDbConfigs.MongoDbDatabaseName);
    var collection = db.GetCollection<Usage>(mongoDbConfigs.MongoDbCollectionName);
  
  // Ensure - Enums are represented as string instead of its index
    if (!BsonSerializer.LookupSerializer(typeof(SignCharacterHeight)).GetType().Equals(typeof(EnumSerializer<SignCharacterHeight>)))
        BsonSerializer.RegisterSerializer(new EnumSerializer<SignCharacterHeight>(MongoDB.Bson.BsonType.String));
    if (!BsonSerializer.LookupSerializer(typeof(SignComponentModule)).GetType().Equals(typeof(EnumSerializer<SignComponentModule>)))
        BsonSerializer.RegisterSerializer(new EnumSerializer<SignComponentModule>(MongoDB.Bson.BsonType.String));

    // Log the data to MongoDb
    await collection.InsertOneAsync(usage);
}
catch (Exception ex)
{
    MessageBox.Show($"Error logging data to MongoDb", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    WriteFailedLogToLocalFile(usage, Properties.Resources.Failed_Logs_File_Path);
}

}