SolvedStackExchange.Redis Could not load file or assembly System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0
✔️Accepted Answer
For anyone else hitting this, these issues prompted me to write up a more complete story on why this happens and what the options for a fix are. I hope this helps anyone hitting this issue resolve it: https://nickcraver.com/blog/2020/02/11/binding-redirects/
Other Answers:
Things to try:
- Reference the
System.Runtime.CompilerServices.Unsafe
library directly as a<PackageReference>
(so VS alerts it needs a binding and offers a one-click fix...sometimes it won't realize this transitively). - If using
app.config
, remove all binding redirects and add a<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
property up top
This is a load errors and I really really really wish they had a wiki page explaining how to fix this because it can plague any library anyway if you step on one of the magical landmines.
- Can you try with the latest version of
System.Runtime.CompilerServices.Unsafe
- 4.7.1 - if you need 4.0.4.0, change manually in ALL of the
app.config
files the bindingRedirect to 4.0.4.0 - and NOT to 4.0.6.0 as your example suggests - To make it clear, these are the assembly versions of System.Runtime.CompilerServices.Unsafe:
- 4.5.0 is 4.0.4.0 (!)
- 4.5.1 is 4.0.4.0 (!)
- 4.5.2 is 4.0.4.0 (!)
- 4.5.3 is 4.0.4.1
- 4.6.0 is 4.0.5.0
- 4.7.0 is 4.0.6.0
- 4.7.1 is 4.0.6.1
Adding this redirect helped to solve the issue in asp.net web application project:
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="4.0.4.1" />
Using .Net 4..7.2 in a .Net web app with Redis windows service - I had same issue afer amending Redis "ConnectionMultiplexer.Connect" connection string to include 'password=...'.
Solved by updating System.Runtime.CompilerService.Unsafe via VS NuGet Package Manager to "latest stable 4.7.0", which showed property version in VS as 4.0.6.0.
Upgrading that way added the following to the web configs -
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
</dependentAssembly>
Hi all,
I have been bashing my head trying to get a fix for a tricky binding error that get thrown when attempting to connect. Incorrect versions of dependent assemblies are being loaded into the process and the problem has been mentioned previously at #1126.
I have used dependency walker and other tools to confirm the correct dependent dlls that should be loaded but for some reason the .net loader resolves these incorrectly.
I have also updated the app.config to redirect to the correct dll version but to no avail and now I am really stuck.
Any help would be most welcome!
Fig