Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing Method Exception MongoDB C# driver

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

like image 443
timkado Avatar asked Jun 08 '26 00:06

timkado


2 Answers

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.

like image 166
craday Avatar answered Jun 10 '26 13:06

craday


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.

like image 42
Alde Avatar answered Jun 10 '26 13:06

Alde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!