3

I am getting the following error with the latest MongoDB C# driver.

System.MissingMethodException MongoDB.Bson.GuidRepresentationMode MongoDB.Bson.BsonDefaults.get_GuidRepresentationMode()

The exception occurs in MongoClient:

var client = new MongoClient("mongodb+srv://____:___@_____.nrzff.mongodb.net/____?retryWrites=true&w=majority");
var database = client.GetDatabase("ResultsModelSummary");
var collection = database.GetCollection<BsonDocument>("Zones");   
var bsonDocList = new List<BsonDocument>();
foreach (var r in resModSums) {
    var bsonDoc = BsonDocument.Parse(Serialization.Serialize<ResultsModelSummary>(r));
    bsonDocList.Add(bsonDoc);
}

collection.InsertManyAsync(bsonDocList);   

public class ResultsModelSummary
    {
        // META
        public string RunID { get; set; } // this is a stringified GUID
        public string TimeStamp { get; set; }
        // more fields .....
    }

The exception actually occurs irregularly. Sometimes the above code works fine.

Any suggestions?

Adding more information:

System.MissingMethodException HResult=0x80131513 Message=Method not found: 'MongoDB.Bson.GuidRepresentationMode MongoDB.Bson.BsonDefaults.get_GuidRepresentationMode()'.
Source=MongoDB.Driver StackTrace: at MongoDB.Driver.MongoUrl..ctor(String url) at MongoDB.Driver.MongoClient..ctor(String connectionString) at EnergyMLGH.MongoDB.<>c__DisplayClass1_0.b__0() in C:\Users...\MongoDB.cs:line 52 at System.Threading.Tasks.Task.Execute()

Screenshot of exception

9
  • 1
    do you use reflection? Commented Aug 27, 2020 at 20:30
  • 1
    what's your use case? I don't think that creating a client itself can trigger this error Commented Aug 27, 2020 at 20:38
  • 1
    I'm still not sure what happens exactly under the scene in your serialization steps. Can you provide the exact code that triggers this issue? Commented Aug 27, 2020 at 21:10
  • 1
    *I mean inner steps from Serialization.Serialize Commented Aug 27, 2020 at 21:13
  • 1
    >The debugger throws the exception on the MongoClient line... wait, you mean that creating MongoClient (the first line in your code) throws? And all other repro lines are not called at all? Commented Aug 27, 2020 at 21:36

2 Answers 2

4

I had the same problem, and it turned out that I had MongoDB references with different versions. I removed the references and added them back, making sure that the versions were all the latest ones and voila, it started working.

Sign up to request clarification or add additional context in comments.

Comments

1

Are you using .NET Aspire? I had the same problem, and here's what I did.

For .NET Aspire's client for MongoDB, there are two package versions :

- Aspire.MongoDB.Driver.v3 (version 9.3.0)

- Aspire.MongoDB.Driver (version 9.3.0)

I had to :

1. Uninstall Aspire.MongoDB.Driver

2. Install Aspire.MongoDB.Driver.v3

3. Rename GuidRepresentationMode to GuidRepresentation in my code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.