.NET Framework app cannot load the Unsafe assembly after a NuGet update
Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe'Short answer
This happens on .NET Framework when one package asks for a different version of the assembly than the one in your bin folder, and no binding redirect joins them. Turn on automatic binding redirects, or add a bindingRedirect to the config file. Test projects and web apps need their own step.
Fix it fast
1Find out which cause you have
Read the full assembly name that is in your output folder. Its version and public key token are what the redirect must point at. Run it in PowerShell from the project folder.
[System.Reflection.AssemblyName]::GetAssemblyName("$PWD\bin\Debug\System.Runtime.CompilerServices.Unsafe.dll").FullName2Match what you see, then jump to its fix
- 1
FileLoadException for the Unsafe assembly after you updated a package that depends on it indirectlyA desktop or service app has automatic binding redirects switched offFix - 2
Automatic redirects are on, or you cannot use them, and the error still names a version that is not in binYou need to write the redirect by handFix - 3
The app runs, but the same code fails with FileLoadException under the test runnerA unit test project gets no redirectsFix - 4
Build warning about assembly reference conflicts in an ASP.NET projectA web app needs the redirect added to web.configFix
- 1
What does the error look like?
Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe'System.IO.FileLoadException
Cause 1 of 4
A desktop or service app has automatic binding redirects switched off
1Check
Open the .csproj and look for AutoGenerateBindingRedirects. Microsoft documents that for .NET Framework 4.5.1 and later the output app.config gets the redirects when this is true. A false value, or a missing value in an older project, leaves two assembly versions unjoined.
findstr /i AutoGenerateBindingRedirects *.csproj2Fix
Set the property to true in the first PropertyGroup, then rebuild. The redirect is written into the output config file, not into your source app.config.
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>3Verify
Rebuild and look at the output config for a bindingRedirect on the assembly.
findstr /i CompilerServices.Unsafe bin\Debug\*.exe.configConfirmed by learn.microsoft.com
Cause 2 of 4
You need to write the redirect by hand
1Check
Compare the version in the error text with the assembly name from the first step. When they differ and no bindingRedirect covers the gap, the runtime refuses the load.
findstr /i bindingRedirect app.config web.config2Fix
Add a dependentAssembly block to the runtime section of the config file. Take name and publicKeyToken from the full name in the first step and put that exact version in newVersion.
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe"
publicKeyToken="PUBLIC_KEY_TOKEN_FROM_STEP_ONE"
culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-NEW_VERSION" newVersion="NEW_VERSION" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>3Verify
Run the app again. The FileLoadException for this assembly no longer appears.
msbuild /t:RebuildConfirmed by learn.microsoft.com
Cause 3 of 4
A unit test project gets no redirects
1Check
Microsoft states that automatic redirect generation only applies to executables. A test project builds to a DLL, so it gets no config file unless you ask for one.
dir bin\Debug\*.dll.config2Fix
Add both properties to the test project and rebuild so a .dll.config with the redirects is written next to the test assembly.
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>3Verify
A .dll.config file now sits beside the test assembly, and the tests load the assembly.
dir bin\Debug\*.dll.configConfirmed by learn.microsoft.com
Cause 4 of 4
A web app needs the redirect added to web.config
1Check
For web apps Visual Studio does not write redirects on its own. It raises a warning for each binding conflict instead.
msbuild /t:Rebuild /v:minimal2Fix
Double-click the conflict warning in the Error List. Visual Studio offers a dialog that adds the needed redirects to the source web.config.
# Visual Studio: Error List > double-click the assembly reference conflict warning > accept the dialog3Verify
web.config now contains a bindingRedirect for the assembly, and the page loads.
findstr /i CompilerServices.Unsafe web.configConfirmed by learn.microsoft.com
How do I stop it coming back?
- Keep AutoGenerateBindingRedirects set to true in every .NET Framework executable project.
- Update NuGet packages for the whole solution together, so one project does not carry an older dependency version.
- Give test projects the two redirect properties from the start.
- After a package update, read the build warnings for assembly conflicts before you run the app.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Databases
Dapper cannot find a constructor that fits the query columns
The error appears when Dapper maps rows into a record or a class whose only constructor takes arguments
Dapper looks for a public parameterless constructor first.
3 causes, each with a fixDatabases
EF Core logs 'An error occurred using the connection to database' with MySQL
Can't connect to MySQL server on 'host_name' (111)
This EF Core message is only a wrapper.
3 causes, each with a fixDatabases
DbContextOptionsBuilder has no UseMySql method
Cannot find the UseMysql method on DbContextOptions
UseMySql comes from a provider package, not from EF Core itself.
3 causes, each with a fixDatabases
There is already an open DataReader on the MySQL connection
The error appears inside a while (reader.Read()) loop
One MySQL connection runs one operation at a time.
3 causes, each with a fixDatabases
C# INSERT fails with 'Fatal error encountered during command execution'
MySql.Data.MySqlClient.MySqlException: Fatal error encountered during command execution
A fatal error during command execution means the connection broke while the INSERT was in flight.
3 causes, each with a fixDatabases
MySqlException: Timeout expired in a .NET app
MySql.Data.MySqlClient.MySqlException: Timeout expired
Timeout expired means a wait ended before it finished.
3 causes, each with a fix