StackExchange.Redis icon indicating copy to clipboard operation
StackExchange.Redis copied to clipboard

Could not load file or assembly System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0

Open lefig opened this issue 7 years ago • 39 comments

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

lefig avatar Apr 19 '19 17:04 lefig

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.

Gimcrack avatar Apr 25 '19 22:04 Gimcrack

Thanks so much for the feedback. I will investigate this further too. Its so annoying!

lefig avatar Apr 27 '19 08:04 lefig

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.

katterfelto avatar May 04 '19 16:05 katterfelto

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...

lefig avatar May 04 '19 19:05 lefig

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.

NickCraver avatar May 04 '19 19:05 NickCraver

Thanks Nick. I really appreciate your help. This sounds like an outstanding suggestion. Must be worth a try!

lefig avatar May 05 '19 09:05 lefig

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.

grisharav avatar May 12 '19 20:05 grisharav

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:)

lefig avatar May 14 '19 20:05 lefig

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)

mgravell avatar May 14 '19 20:05 mgravell

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.

katterfelto avatar May 15 '19 22:05 katterfelto

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

BCalpe avatar May 26 '19 19:05 BCalpe

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.

lefig avatar May 27 '19 22:05 lefig

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?

BCalpe avatar May 28 '19 07:05 BCalpe

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.

lefig avatar May 29 '19 19:05 lefig

I solved it by downgrading to 4.5.1 and that helped. Unfortunately no other solution helped.

BCalpe avatar May 30 '19 21:05 BCalpe

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"/>

kasilioglu avatar May 31 '19 07:05 kasilioglu

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.

hvbnet avatar Jun 02 '19 04:06 hvbnet

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.

lefig avatar Jun 04 '19 22:06 lefig

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" />

yury-kozlov avatar Jun 13 '19 14:06 yury-kozlov

Much appreciated. Other users too who may encounter this bizarre problem will be able to try various potential fixes.

lefig avatar Jun 13 '19 20:06 lefig