4
votes

I created a MongoLab sandbox database. I connected with MongoChef and it works fine.

I installed MongoDB.Driver 2.2.2 through Nuget.

I made some simple C# demo code, but just don't make it work.

The connection string was copied straight from MongoChef where it definitely works!

When trying the same with a local Mongo instance I get the same behaviour. But when trying with driver version 2.1.1 it works locally and with MongoLab! Is the internal connection behaviour changed in some way in 2.2?

    const string connectionString = "mongodb://user:[email protected]:47095/dbname";

    var client = new MongoClient(connectionString);

    var db = client.GetDatabase("dbname");
    var skills = db.GetCollection<Skill>("skill");
    skills.InsertOne(new Skill {SkillName = "TEST"});

This is the exception thrown:

A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = WritableServerSelector, 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", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.MissingMethodException: Method not found: 'MongoDB.Bson.BsonDocument MongoDB.Bson.RawBsonDocument.Materialize(MongoDB.Bson.IO.BsonBinaryReaderSettings)'. at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol1.ProcessReply(ConnectionId connectionId, ReplyMessage1 reply) at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.d__11.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MongoDB.Driver.Core.Connections.ConnectionInitializer.d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MongoDB.Driver.Core.Connections.BinaryConnection.d__47.MoveNext() --- End of inner exception stack trace --- at MongoDB.Driver.Core.Connections.BinaryConnection.d__47.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) at MongoDB.Driver.Core.Servers.ClusterableServer.d__42.MoveNext()" }] }.

5
When desperately trying everything I also switched to different driver version. To my surprise everything works again when going back to version 2.1.1. But the succeeding version 2.2.0 does NOT work. I still have no explanation as to why.Jakob Lithner
The same problem also occurs when connection to local MongoDb instance.Jakob Lithner

5 Answers

7
votes

I had the same problem and found the possible solution: check version of following packages: MongoDB.Driver, MongoDB.Driver.Core, MongoDB.Bson in all your projects in solution. Maybe some packages have different versions, then update them to latest version. Also update your MongoDB to latest version. Hope this helps.

1
votes

I had faced similar issues while I was doing some POC with .net core, docker and mongo DB. Turns out to be the mongodb connection string issues. After replacing the "localhost" with the IP, the issue got resolved. But this was working fine with IIS.

Fix

version: '3.4'

services:

reverseproxy_mongodemoapp: image: rnd/reverseproxy_mongodemoapp build: context: . dockerfile: src/reverseproxy/Docker_reverseproxy volumes: - ./var/run/docker.sock:/tmp/docker.sock:ro ports: - 5500:5500 networks: - mongodemoappdockernat restart: always

mongodemoapp: image: rnd/mongodemoapp:${TAG:-latest} build: context: . dockerfile: src/mongodemoapp/Dockerfile depends_on: - reverseproxy_mongodemoapp networks: - mongodemoappdockernat
restart: always environment: - ASPNETCORE_ENVIRONMENT=development - BooksCollectionName=${APP_MONGODB_COLLECTION:-Books} - ConnectionString=${APP_MONGODB_CONNECTION:-mongodb://192.168.0.105:27017/} - DatabaseName=${APP_MONGODB:-BookStore}

networks: mongodemoappdockernat: driver: bridge

0
votes

Try connection string without database name:

string connectionString = "mongodb://user:[email protected]:47095";
0
votes

For me, I used the driver in a web project and it was all working well. I then refactored those into another project, but forgot to remove the MongoDB library references in the web project. It took a while, but worked after removing the dll references from the web project. Hope it helps someone.

0
votes

Actually I have the same problem with free hosting for MongoDB from mlab.com. Mongo compass connect succesfully. Driver version 2.7.2, latest version from available for VisualStudio. Problem solved by changing hosting provider to MondoBD.Atlas.