Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

.NET Framework app cannot load the Unsafe assembly after a NuGet update

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

  • Applies to .NET Framework 4.8, Visual Studio 2022, NuGet PackageReference
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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").FullName
  2. 2Match what you see, then jump to its fix

    1. 1FileLoadException 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. 2Automatic 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. 3The app runs, but the same code fails with FileLoadException under the test runnerA unit test project gets no redirectsFix
    4. 4Build warning about assembly reference conflicts in an ASP.NET projectA web app needs the redirect added to web.configFix

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

2Fix

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

Confirmed by learn.microsoft.com

Did this fix it?

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

2Fix

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

Confirmed by learn.microsoft.com

Did this fix it?

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

2Fix

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

Confirmed by learn.microsoft.com

Did this fix it?

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

2Fix

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 dialog

3Verify

web.config now contains a bindingRedirect for the assembly, and the page loads.

findstr /i CompilerServices.Unsafe web.config

Confirmed by learn.microsoft.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.