The meaning of life is to explore the world

MongoDB upgrade error in .NET

Posted on By Jason Liu

Upgrading MongoDB driver in .NET project causing dependent assembly error

Error message

  Calling constructor xxx.yyy()
 ---> System.TimeoutException: A timeout occurred after 30000ms selecting a server using CompositeServerSelector{ Selectors = WritableServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }, OperationsCountServerSelector }. Client view of cluster state is { ClusterId : "1", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27017" }", EndPoint: "Unspecified/localhost:27017", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

troubleshoot
- Referencing the new library System.Runtime.CompilerServices.Unsafe.dll or any Mongo related libraries in the IDE doesn’t help, either via NuGet installation or direct file reference.
my_image
- Clearing the NuGet cache (as in %USERPROFILE%.nuget) doesn’t help.
my_image
- obj\project.assets.json shows the resolved dependencies:

  "System.Threading.Tasks.Extensions/4.5.4": {
	"type": "package",
	"dependencies": {
	  "System.Runtime.CompilerServices.Unsafe": "4.5.3"
	},
	"frameworkAssemblies": [
	  "mscorlib"
	],
	"compile": {
	  "lib/net461/System.Threading.Tasks.Extensions.dll": {}
	},
	"runtime": {
	  "lib/net461/System.Threading.Tasks.Extensions.dll": {}
	}
  },
  ......
  "System.Memory/4.5.5": {
  "type": "package",
  "dependencies": {
	"System.Buffers": "4.5.1",
	"System.Numerics.Vectors": "4.5.0",
	"System.Runtime.CompilerServices.Unsafe": "4.5.3"
  },
  ......
  "System.Threading.Tasks.Extensions/4.5.4": {
	"type": "package",
	"dependencies": {
	  "System.Runtime.CompilerServices.Unsafe": "4.5.3"
	},
	"frameworkAssemblies": [
	  "mscorlib"
	],
	"compile": {
	  "lib/net461/System.Threading.Tasks.Extensions.dll": {}
	},
	"runtime": {
	  "lib/net461/System.Threading.Tasks.Extensions.dll": {}
	}
  },

cause
.NET framework libraries are using an old version of System.Runtime.CompilerServices.Unsafe.dll e.g. 4.5.3, while MongoDB.Driver.Core is using a new version 5.0.0. NuGet resolves this conflict to the old version. It may happen again when there’s an upgrade in packages (MongoDB) or framework (.NET).

solution
Add binding redirect to each project referencing the project having the Mongo upgrade to force it to use higher version e.g. 6.0.0.0.
Follow the blog: https://nickcraver.com/blog/2020/02/11/binding-redirects/
Note that assembly version differs from NuGet package version, e.g. (4.0.4.1 ==> 4.5.3)