I’m trying to connect to my MongoDB database using the following setup:
public ReplybotServiceLayer(MongoDbSettings databaseSettings)
{
var connectionString = $"mongodb+srv://{databaseSettings.User}:{databaseSettings.Password}@{databaseSettings.Cluster}.mongodb.net/{DatabaseName}?w=majority&connect=replicaSet";
var client = new MongoClient(connectionString);
var database = client.GetDatabase(DatabaseName);
_guildResponsesCollection = database.GetCollection<GuildResponseEntity>("responses");
_guildConfigurationsCollection = database.GetCollection<GuildConfigurationEntity>("configuration");
}
public async Task<IList<TriggerResponse>?> GetResponsesForGuild(ulong guildId)
{
var filter = Builders<GuildResponseEntity>.Filter.Eq("guildId", guildId.ToString());
var guildResponses = await _guildResponsesCollection.Find(filter).FirstOrDefaultAsync();
var triggerResponses = guildResponses?.Responses;
return triggerResponses?.Select(tr => tr.ToDomain()).ToList();
}
This connection works when running my app locally, but when I publish my app to Azure, the endpoint times out after about 30 seconds with the following error:
"A timeout occurred after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }, OperationsCountServerSelector }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [] }."
I even tried temporarily enabling "access from anywhere" in MongoDB Atlas, and it didn’t work, so it shouldn’t be a network access issue in theory.
What could be causing this time out? Is there any way for me to check for more details?
2
Answers
The answer I ended up coming to was rather silly - the secrets had not properly saved in the configuration on Azure so it was legitimately unauthorized. I think there's helpful suggestions in the answers and comments here otherwise if you're running into a similar problem and happening upon this post.
Writing it as answer since it’s quite big.
Your issue is that for some reason, driver doesn’t see any server (you can see it in this part of error message
..Servers : []..
). It’s impossible to know what is wrong in your particular case without getting more details from events (Most likely something wrong in server or dns configuration). The best way is to look atClusterRemovingServerEvent
event which contains a removing Reason field. But as I mentioned above, something can be visible fromServerDescriptionChangedEvent
event andClusterDescriptionEventChanged
event too.There are a number of ways to get such details:
SdamLogFilename:
Create a custom event subscriber. See here. There, you will have full control over what events to subscribe on and how gather and print them.
The above ways are obsoleted, but still work.