Could not load file or assembly System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0
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
I was having the same issue. It turned out that my install project had been bundling a newer version of a dependency (ServiceStack.Text) than what I had specified through NuGet. Deleting the offending versions of the package from my .nuget store fixed the issue.
Thanks so much for the feedback. I will investigate this further too. Its so annoying!
I am having the same issue, to compound the problem this is my first experimentation with Redis. I have cleared everything from my nuget cache. It seems to work with a dotnetcore 2.2 console application. However any windows desktop application throws this exception. I have tried VS2017 (.net 4.7 and 4.6.2 applications) also VS2019 (.net 4.7.2).
VS2017 downloaded the source and the exception is being thrown on line 948 of CreateMultiplexer.cs:
var task = muxer.ReconfigureAsync(true, false, logProxy, null, "connect");
I am specifying a password my connection code is:
ConfigurationOptions cfg = new ConfigurationOptions()
{
EndPoints =
{
{ "raspberrypi3", 6379 }
},
Password = "******"
};
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(cfg);
I'm not sure where to go from here.
Snap. I am truly stuck. I have investigated all the dependencies using tools like dependency walker and made sure they are correct. But the problem seems to be with the .net loader and reflection and triggered on that line you mention. Its perplexing...
Things to try:
- Reference the
System.Runtime.CompilerServices.Unsafelibrary 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.
Thanks Nick. I really appreciate your help. This sounds like an outstanding suggestion. Must be worth a try!
Did anyone manage to resolve this? We are having the same issue and suggested things to try had no effect. Note - we are using .net framework 4.7.2. All works fine when working with .net core.
No unfortunately. We are on 4.6 but tried 4.7.1 to no effect. I noticed that even if you force the correct dlls to load into process that the framework loader still throws the exception on the line mentioned above. This is seriously not funny now:)
Please see #1130, which includes the binding redirect and a workaround using 4.6-preview (the bug in Unsafe is fixed in 4.6, but it isn't released yet)
Thanks Mark that solved my issue. I installed System.Runtime.CompilerServices.Unsafe version 4.6.0-preview6.19259.10 from NuGet into my .NET Framework v4.6.1 project and that fixed that project.
Hi
I'm getting the same error: Could not load file or assembly System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0. I'm building a .NET Framework 4.6.1 Class Library project that need to be uploaded to inRiver.
I tried adding the line to my csproj file but it didn't help
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.6.0-preview6.19259.10" />
Do I need to remove the Reference to the older System.Runtime.CompilerServices.Unsafe ? Any other step I may need to do before adding the above line to my csproj file?
Thanks
Thanks Mark that solved my issue. I installed System.Runtime.CompilerServices.Unsafe version 4.6.0-preview6.19259.10 from NuGet into my .NET Framework v4.6.1 project and that fixed that project.
Likewise, thank you Mark for all your help and patience. I'll give it a good try and will cross my fingers.
After installing Stackexchange.Redis I have upgraded it to System.Runtime.CompilerServices.Unsafe version 4.6.0-preview6.19259.10. After deploying to inRiver I still get the same error.
Do I need to change something else?
Have you checked what dll/assembly is actually being loaded into memory? Using VS debug tools you can check the path and assembly version number.
I solved it by downgrading to 4.5.1 and that helped. Unfortunately no other solution helped.
Not so sure to write to this issue but it seems related. I upgraded to latest version and yes i got same problem with this assembly version. Plus; I also got an other error with System.Net.Http and required to add binding redirection. To ensure its not me; i just created a MVC+WebApi solution in VS2017 and .Net version 4.6.1 and it fails at Web api help page complaining System.Net.Http.HttpMethod System.Web.Http.Description.ApiDescription.get_HttpMethod()
Following bindingRedirection fixes;
<assemblyIdentity name="System.Net.Http" publicKeyToken="B03F5F7F11D50A3A" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0"/>
I using .Net 4.7.2 and have same issue, It work fine in development mode but when publish have same issue. Unfortunately no other solution helped.
There is a bug in VS concerning publish not actually "publishing" all dependencies for .net Core apps. I am not sure about .net Framework though. I hit that problem and found that some dependencies were actually missing from the release folder.
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" />
Much appreciated. Other users too who may encounter this bizarre problem will be able to try various potential fixes.