'Problems with mongo csharp driver version

I use the mongo csharp driver version 1.9.1. But, I have a problem with this dll. The exception is:

Could not load file or assembly 'MongoDB.Bson, Version=1.9.1.221, Culture=neutral, PublicKeyToken=f686731cfb9cc103' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"MongoDB.Bson, Version=1.9.1.221, Culture=neutral, PublicKeyToken=f686731cfb9cc103.

Does anybody have any suggestion ?

Thanks a lot in advance



Solution 1:[1]

I was having the same problem, this error occurs due to conflict you may have added more than one reference to MonogoDb in your Solution. Solution: If there are more than one project in your solution remove MongoDb references from all project and then add to only one project that is dependent on all other projects. It worked for me.

Solution 2:[2]

To build on Amanullah Tariq's answer; You may have more than one project referencing MongoDb or its drivers, HOWEVER; [solution] make sure that each project make use of the same version. That was what was causing my error. If the versions are not the same, make sure to either upgrade or downgrade the versions to match. I hope this helps any one struggling with this issue in the future.

Solution 3:[3]

For me, it was not possible to remove the MongoDB references from all the projects and refer to single project, as it's a utility solution and works on different .net frameworks and .net core versions.

So, I have updated the mongodb driver for the project using nuget and it worked.

Other projects are also working fine. As, MongoDB Drivers gets the reference from the common place.

Before Update of nuget:

enter image description here

After Update of nuget:

enter image description here

If any of the project fails then just update MongoDB.Driver version to the latest.

For .Net Framework:

Check your packages.config file, It will be something like this (sample for 4.7.2 framework):

<package id="MongoDB.Driver" version="2.12.0" targetFramework="net472" />

For .Net Core: check in .csproj file in <ItemGroup>. Something like this:

<ItemGroup>
   ...... // other are deleted just to focus on main part
   <PackageReference Include="MongoDB.Driver" Version="2.12.0" />
</ItemGroup>

Solution 4:[4]

I have another odd ball solution, in case the others didn't help. My issue was that when I would remove the nuget package. It would not remove the dlls from the bin folder. So even if you installed the new package. The dlls in the bin folder were still from the old one.

Solution

  1. First remove the nuget package
  2. Right click the project and do a "Clean"
  3. Then go into the bin folder and make sure its empty. If not delete everything in it.
  4. Finally install the new nuget package.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amanullah Tariq
Solution 2 onmyway
Solution 3
Solution 4 Donny V.