I'm completely stumped. I am using the latest c# drivers (2.3.0.157) and the latest MongoDB (3.2). The DB is running as a standalone setup with no replication or sharding. I've tried running locally on Windows as well as remotely on Amazon LINUX.
I continue to get a timeout error but mysteriously sometimes it just works (maybe once every 20 - 30 attempts).
I am creating the connection as such:
private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["MongoDB"].ToString();
private static readonly string DataBase = ConfigurationManager.ConnectionStrings["MongoDBDatabase"].ToString();
private static IMongoDatabase _database;
public static IMongoDatabase GetDatabase(string database)
{
if (_database == null)
{
var client = new MongoClient(ConnectionString);
_database = client.GetDatabase(database);
}
return _database;
}
And calling it like this:
public static List<Earnings> GetEarnings()
{
var db = GetDatabase(DataBase);
if (db == null) return new List<Earnings>();
var logs = db.GetCollection<Earnings>("EarningsData");
var all = logs.Find(new BsonDocument()).ToEnumerable().OrderBy(x => x.Symbol).ToList();
return all;
}
And it'll time out on the logs.Find part of the method. Here's the full message:
Additional information:
A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode = Primary, TagSets = [] } }, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Direct", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "XX.XX.XX.XX:27017" }", EndPoint: "XX.XX.XX.XX:27017", State: "Disconnected", Type: "Unknown" }] }.
I've tried using the fully qualified host name, adding connect=direct and connect=replicaSet to the connection string, using MongoClientSettings instead of the connection string and everything else I could possibly find on forums and StackOverflow. I'm at a loss and not even sure where to look next. Any advice?
I should also add, I can connect fine from the command line and RoboMongo...