I created a Visual Studio (Community 2019) project with C# using ServiceStack.Redis
. Since it is C#, I use Windows 10 (there is a Redis version for Windows but it is really old and as I know, it is unofficial so I am afraid that might be the problem).
Here is an excerpt from my code:
public class PeopleStorage: IDisposable
{
public PeopleStorage()
{
redisManager = new RedisManagerPool("localhost");
redis = (RedisClient)redisManager.GetClient();
facts = (RedisTypedClient<List<Fact>>)redis.As<List<Fact>>();
}
public List<Fact> GetFacts(int id)
{
string sid = id.ToString();
if (facts.ContainsKey(sid))
return facts[sid];
return accessor.GetFacts(id);
}
private RedisTypedClient<List<Fact>> facts;
private RedisClient redis;
private RedisManagerPool redisManager;
}
In an attempt to connect to Redis in line return facts[sid];
, an exception occurs:
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 it’s
dependences. The found Assembly’s manifest definition does not match
the Assembly reference. (Exception from HRESULT: 0x80131040)"
(May be inaccurate as I have translated it)
I have tried updating all the packages, starting with ServiceStack
packages, ending with System.Runtime.CompilerServices.Unsafe
itself. Moreover, you can’t choose 4.0.4.1 version in NuGet, the closest one there is 4.0.0, while the relevant is 4.0.7.
I do not understand why it uses this version and how I can fix this problem.
Even a clean reinstall of Visual Studio did not help.
12
Answers
I assume that you’re using the .NET Framework. This error is known for
ServiceStack.Redis
and is tracked on GitHub. It occurs because you use libraries that depend on different versions ofSystem.Runtime.CompilerServices.Unsafe
. These transitive dependencies need to be resolved and consolidated to end up with one assembly in your output folder. You will end up with the latest of these versions. Consequently, if one of the libraries depends on a specific version that is older, it will not be found.The bug that causes this issue is fixed in
System.Runtime.CompilerServices.Unsafe
4.6.0
. Use binding redirects, to load the specific version of the assembly that you need. Insert this snippet into all of yourapp.config
files.You need to specify the assembly version of the assembly that you need as
newVersion
. This is not the same as the package version that you choose when installing your NuGet package. They correspond like this:In this binding redirect I use the newer version of
System.Runtime.CompilerServices.Unsafe
that fixes the bug. However, if you depend on the older version, use4.0.4.1
.It seems that you have installed System.Runtime.CompilerServices.Unsafe nuget package
4.5.3
version. And it corresponds toSystem.Runtime.CompilerServices.Unsafe.dll
assembly version4.0.4.1
.Suggestion
1) Please try to register
System.Runtime.CompilerServices.Unsafe
version4.0.4.1
into GAC so that the system can it.Run Developer Command Prompt for VS2019 as Administrator
type:
2) If you use Net Framework projects with
xxx.config
file, you could use bindingRedirect.Add these in
app.config
file orweb.config
file:Besides, if you update
System.Runtime.CompilerServices.Unsafe
nuget package version to the newer version, you should also changed the bindingRedirect assembly version.You can refer to these assembly versions of
System.Runtime.CompilerServices.Unsafe
4.5.x
isSystem.Runtime.CompilerServices.Unsafe
nuget package version while4.0.x.x
isSystem.Runtime.CompilerServices.Unsafe.dll
assembly version.In my experience, I didn’t need to install System.Runtime.CompilerServices.Unsafe because it is referenced from another package, npgsql.
Instead, what I did is as follow:
This is the solution I found so far, to add dependentAssembly bindingReference
Based on Perry’s answer, I simply installed the nuget package System.Runtime.CompilerServices.Unsafe version 4.5.3 and the problem was solved.
Recently I encountered exactly the same error message. However, that message did not appear in all PCs that I used to test my app. Some PCs yielded the error message but some others are not. I couldn’t differentiate which are the characteristics of PC that would be generating error message and which ones did not. It seemed.. random.
Then I noticed a warning message when I compiled my app (I was using Visual Studio 2019 at the time):
And I did exactly what it told me:
Then the problem was solved.
My problem was solved by deleting the "bin" and "obj" folders.
Thanks @thatguy finally was able to resolve the issue.
The solution that worked for me was from Nick Craver with details as below:
In my case, a Visual Studio update helps.
My app is a native EXE which loads .NET assemblies; we have no app.config file. Assembly config files are not loaded and used at runtime. So the approach I took to solve this was based on helpful answers here and here.
The idea is to catch the assembly load exception and handle it by substituting whatever existing (higher versioned)
System.Runtime.CompilerServices.Unsafe
assembly is already loaded.Add this somewhere early in application startup:
And this is the event handler:
This worked great in my situation. I also happen to think this is a little more discoverable than potentially cryptic settings in
app.config
(IMO) so it might be a good solution in general.In my case, everything was working but I started getting this message after a merge.
None of the solutions worked because everything was setup correctly. My dll version was 6.0, so I downgraded to 5.0, ran the project (successfully) and then upgraded to 6.0 and then ran it again. Worked perfectly.
Hope this helps someone.
In my case I installed the latest 6.0.0.0 package across my solution (including the net 472 web apps!) and then added a binding redirect to version 6
In my case, the package/assembly reference entry was not present in the web.config file at all.