2

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.

2
  • Have you added mongocsharpdriver packge to your project?
    – Vivek Nuna
    Commented Dec 4, 2021 at 14:59
  • Your above code doesn't use Legacy driver, it looks like something different is wrong on your side. Create a simple console application and check this behavior there. I think it will work without any issues, then check the difference with the main app
    – dododo
    Commented Dec 4, 2021 at 16:08

1 Answer 1

1

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.

6
  • Thanks! I've added it through the NuGet packages, but I doesn't seem to make a differenece... any way to test if I've done it correctly...? Commented Dec 5, 2021 at 16:43
  • @YafimSimanovsky have you added the dependencies as mentioned in the given link in my answer?
    – Vivek Nuna
    Commented Dec 5, 2021 at 17:26
  • Yes it appears in the packages.config file in the project if that's what you mean. I don't want to install it manually via a CLI because the project is to be compiled for external users and i'm not sure how they will use it without installing it manually, so I must include it as some package within the project compilation process (such as NuGet). Commented Dec 6, 2021 at 16:30
  • What do you mean exactly by 'consider the .net standard 2.0 dependencies'? perhaps i'm missing some file because I still can't solve the issue... Commented Dec 14, 2021 at 18:22
  • @YafimSimanovsky is this issue resolved or still facing?
    – Vivek Nuna
    Commented Nov 22, 2022 at 18:24

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.