简体   繁体   中英

C# .Net + MongoDB Atlas connection string MongoDB.Driver.Legacy error when trying to run Client

I've set up a Cluster + Collection in a MongoDB Atlas database. I'm trying to follow tutorials such as this one: Bookstore | Using ASP.NET Core 5.0 and MongoDB

my code is as follows, with <> replaced with real data of course:

var client = new MongoClient("mongodb+srv://<username>:<password>@<clustername>.ck3ra.mongodb.net/<database>?retryWrites=true&w=majority");
var database = client.GetDatabase("<database>");
var collection = database.GetCollection<BsonDocument>("validation");
ShowMessage(collection.ToString());

The error i'm getting is:

System.IO.FileLoadException: Could not load file or assembly 'MongoDB.Driver.Legacy' or one of its dependencies. An argument was out of its legal range. (Exception from HRESULT: 0x80131502) File name: 'MongoDB.Driver.Legacy' ---> System.ArgumentOutOfRangeException: Length cannot be less than zero. Parameter name: length

I'm not sure what's wrong here because I am writing the connection string exactly as per the Connect instructions on the MongoDB website, as well as the other tutorials...

Would appreciate any help on this. Thanks.

You need to make sure that your have added the mongocsharpdrive package to your package and it's dependencies. As you are targeting to .net5 so please consider the .net standard 2.0 dependencies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM