3

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...

kenorb
  • 155,785
  • 88
  • 678
  • 743
user1464246
  • 125
  • 1
  • 2
  • 9

2 Answers2

3

We finally figured out how to work around this issue but I still don't understand what's happening. In our case, we have a server that spawns ~10 signalr hubs that get their data from MongoDB. It seems that when the app was starting up it was making several rapid calls to MongoDB to get the initial set of data and while it would occasionally worked, most times it didn't. We ended up solving this by adding a one second delay between loading each SignalR hub so the initial query was delayed a bit and we didn't have contention.

The weird thing about this is none of these collections have a large amount of data and the initial load is usually < 100 documents per collection (max). Once things are initialized it doesn't seem to matter how often we hit MongoDB. It just seems to be on the initial load.

user1464246
  • 125
  • 1
  • 2
  • 9
  • My mongo db server is running in this port 6056. but the error still showing the default endpoint port. Please let me know if there is any solution.Error Statement: "because the target machine actively refused it 127.0.0.1:27017" – mRhNs13 Oct 19 '18 at 12:15
0

An old topic but I found I was getting a similar error (2.11.0-beta2, netcoreapp3.1) and then I realised DocumentDb is restricted to connectivity within the same VPC. It's mentioned here.

https://docs.aws.amazon.com/documentdb/latest/developerguide/connect-from-outside-a-vpc.html

Amazon DocumentDB (with MongoDB compatibility) clusters are deployed within an Amazon Virtual Private Cloud (Amazon VPC). They can be accessed directly by Amazon EC2 instances or other AWS services that are deployed in the same Amazon VPC. Additionally, Amazon DocumentDB can be accessed by EC2 instances or other AWS services in different VPCs in the same AWS Region or other Regions via VPC peering.

Check you're in the same VPC. If not, good luck.

Adam C
  • 86
  • 1
  • 4