I have a 3 projects (.sln) that are their own project plus a secondary project that has the MongoDB code in it.
On the same computer with the 3 visual studio project open calling the same methods only 1 of them work the other all throw and error. Since it's the same line of code it mean there is a setting in the main project that mongo doesn't seems to like. Any idea what this might be.
here's the method in question in the shared project for mongo:
public static class MongoTool
{
public static bool DoesCollectionExist(string username, string password, string uri, string port, string database, string collectionName)
{
// get the client
var client = new MongoClient($@"mongodb://{username}:{password}@{uri}:{port}");
// get the database
var db = client.GetDatabase(database);
// create the filter for collection names
var filter = new BsonDocument("name", collectionName);
var options = new ListCollectionNamesOptions { Filter = filter };
// return if the filter return at least 1 record
return db.ListCollectionNames(options).Any();
}
}
in one of the project a dummy console application i have the following call and it works, it returns true.
namespace ConsoleApp1781
{
class Program
{
static void Main(string[] args)
{
var test = MongoTool.DoesCollectionExist("admin", "admin", "127.0.0.1", "27017", "SampleSet", "TemperatureProbe");
}
}
}
In another project winform app i have this code (the third project use similar but have same error) :
private void CustomMongoSaveTest(Probe temperatureProbe)
{
if (Environment.UserName.Contains("tech"))
{
// check if collection exist
var collectionExist = MongoTool.DoesCollectionExist("admin", "admin", "127.0.0.1", "27017", "SampleSet", "TemperatureProbe");
// if the collection exists
if (collectionExist)
{
// create the bson document
var bsonDoc = MongoTool.ConvertToBsonDocument(temperatureProbe);
// insert in collection
MongoTool.SaveDocument(bsonDoc, "admin", "admin", "127.0.0.1", "27017", "SampleSet", "TemperatureProbe");
}
}
}
Although being the same exact code running on the same computer that second one is throwing the following exception. Having the same project in attach to all solution i can easily follow the code and the exception occur on the line :
db.ListCollectionNames(options).Any();
With the exception being :
A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "127.0.0.1:27017" }", EndPoint: "127.0.0.1:27017", ReasonChanged: "ServerInitialDescription", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", LastHeartbeatTimestamp: null, LastUpdateTimestamp: "2020-08-07T16:00:54.4780565Z" }] }.
After adding extra methods for different handling like Collection Create/Delete, none of the works except when inside a console application. all project uses nuget MongoDB.Driver 2.11.0
I managed to check the difference in the log file when i run the line and it crash and this the difference in the Mongo logs :
{"t":{"$date":"2020-08-07T12:21:30.806-04:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn243","msg":"connection ended","attr":{"remote":"127.0.0.1:60792","connectionCount":5}}
{"t":{"$date":"2020-08-07T12:21:30.808-04:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn244","msg":"connection ended","attr":{"remote":"127.0.0.1:60793","connectionCount":4}}
{"t":{"$date":"2020-08-07T12:21:30.808-04:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn247","msg":"connection ended","attr":{"remote":"127.0.0.1:60796","connectionCount":3}}
{"t":{"$date":"2020-08-07T12:21:30.808-04:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn245","msg":"connection ended","attr":{"remote":"127.0.0.1:60794","connectionCount":2}}
{"t":{"$date":"2020-08-07T12:21:31.951-04:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn246","msg":"connection ended","attr":{"remote":"127.0.0.1:60795","connectionCount":1}}
{"t":{"$date":"2020-08-07T12:23:02.293-04:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn242","msg":"connection ended","attr":{"remote":"127.0.0.1:60791","connectionCount":0}}
When calling the same line form the app that works the log is different, i have this :
{"t":{"$date":"2020-08-07T12:31:02.088-04:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"connection accepted","attr":{"remote":"127.0.0.1:61402","sessionId":248,"connectionCount":1}}
{"t":{"$date":"2020-08-07T12:31:02.093-04:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"connection accepted","attr":{"remote":"127.0.0.1:61403","sessionId":249,"connectionCount":2}}
{"t":{"$date":"2020-08-07T12:31:02.136-04:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn249","msg":"client metadata","attr":{"remote":"127.0.0.1:61403","client":"conn249","doc":{"driver":{"name":"mongo-csharp-driver","version":"2.11.0.0"},"os":{"type":"Windows","name":"Microsoft Windows 10.0.18362","architecture":"x86_64","version":"10.0.18362"},"platform":".NET Framework 4.8.4180.0"}}}
{"t":{"$date":"2020-08-07T12:31:02.137-04:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn248","msg":"client metadata","attr":{"remote":"127.0.0.1:61402","client":"conn248","doc":{"driver":{"name":"mongo-csharp-driver","version":"2.11.0.0"},"os":{"type":"Windows","name":"Microsoft Windows 10.0.18362","architecture":"x86_64","version":"10.0.18362"},"platform":".NET Framework 4.8.4180.0"}}}
{"t":{"$date":"2020-08-07T12:31:02.235-04:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"connection accepted","attr":{"remote":"127.0.0.1:61404","sessionId":250,"connectionCount":3}}
{"t":{"$date":"2020-08-07T12:31:02.244-04:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn250","msg":"client metadata","attr":{"remote":"127.0.0.1:61404","client":"conn250","doc":{"driver":{"name":"mongo-csharp-driver","version":"2.11.0.0"},"os":{"type":"Windows","name":"Microsoft Windows 10.0.18362","architecture":"x86_64","version":"10.0.18362"},"platform":".NET Framework 4.8.4180.0"}}}
{"t":{"$date":"2020-08-07T12:31:02.297-04:00"},"s":"I", "c":"ACCESS", "id":20250, "ctx":"conn250","msg":"Successful authentication","attr":{"mechanism":"SCRAM-SHA-256","principalName":"admin","authenticationDatabase":"admin","client":"127.0.0.1:61404"}}
{"t":{"$date":"2020-08-07T12:31:04.835-04:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn250","msg":"connection ended","attr":{"remote":"127.0.0.1:61404","connectionCount":1}}
{"t":{"$date":"2020-08-07T12:31:04.835-04:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn248","msg":"connection ended","attr":{"remote":"127.0.0.1:61402","connectionCount":2}}
{"t":{"$date":"2020-08-07T12:31:05.334-04:00"},"s":"W", "c":"NETWORK", "id":4615610, "ctx":"conn249","msg":"Failed to check socket connectivity","attr":{"error":"The operation completed successfully."}}
{"t":{"$date":"2020-08-07T12:31:05.334-04:00"},"s":"I", "c":"-", "id":20883, "ctx":"conn249","msg":"Interrupted operation as its client disconnected","attr":{"opId":4183920}}
{"t":{"$date":"2020-08-07T12:31:05.334-04:00"},"s":"I", "c":"NETWORK", "id":22989, "ctx":"conn249","msg":"Error sending response to client. Ending connection from remote","attr":{"error":{"code":6,"codeName":"HostUnreachable","errmsg":"Connection reset by peer"},"remote":"127.0.0.1:61403","connectionId":249}}
{"t":{"$date":"2020-08-07T12:31:05.334-04:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn249","msg":"connection ended","attr":{"remote":"127.0.0.1:61403","connectionCount":0}}