Could not load file or assembly ‘System.Diagnostics.DiagnosticSource’ / ‘System.Runtime.CompilerServices.Unsafe’

Typical Error Message:

System.Diagnostics.DiagnosticSource

System.IO.FileLoadException: 'Could not load file or assembly 
'System.Diagnostics.DiagnosticSource, Version=6.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies.
The located assembly's manifest definition does not match the assembly reference. 
(Exception from HRESULT: 0x80131040)'

System.Runtime.CompilerServices.Unsafe

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 its dependencies.
The located assembly's manifest definition does not match the assembly reference.
(Exception from HRESULT: 0x80131040)'

How To Fix

At Windows File Explorer, open the file “<project-name>.csproj“.

Go to the Reference section, you will find something like this:

<Reference Include="System.Diagnostics.DiagnosticSource, Version=7.0.0.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.DiagnosticSource.7.0.2\lib\net462\System.Diagnostics.DiagnosticSource.dll</HintPath>
</Reference>


<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>

Obtain the version number and PublicKeyToken, such as:

System.Diagnostics.DiagnosticSource

  • Version: 7.0.0.2
  • PublicKeyToken: cc7b13ffcd2ddd51

System.Runtime.CompilerServices.Unsafe

  • Version: 6.0.0.0
  • PublicKeyToken: b03f5f7f11d50a3a

Edit the file web.config, and fill in the version number and publicKeyToken into the following lines:

<runtime>
	<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
		<dependentAssembly>
			<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
			<bindingRedirect oldVersion="0.0.0.0-7.0.0.2" newVersion="7.0.0.2" />
		</dependentAssembly>
	</assemblyBinding>
	<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
		<dependentAssembly>
			<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
			<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
		</dependentAssembly>
	</assemblyBinding>
</runtime>

The assembly binding block format:

<runtime>

	<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
		<dependentAssembly>
			<assemblyIdentity name="...assembly name..." 
			          publicKeyToken="...public key token..." culture="neutral" />
			<bindingRedirect oldVersion="0.0.0.0-version number" 
			          newVersion="...version number..." />
		</dependentAssembly>
	</assemblyBinding>
	
</runtime>