Skip to content
Latchkey

dotnet "Could not load file or assembly" in CI

At run time (often during dotnet test) the runtime tried to load an assembly that is missing, or found a different version than was compiled against. It throws FileNotFoundException or FileLoadException and the process fails.

What this error means

A test run or app start fails with "Could not load file or assembly ... or one of its dependencies", naming the assembly and sometimes a version. The build succeeded; the failure is at load time.

Test/run output
System.IO.FileLoadException: Could not load file or assembly
'System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
The located assembly's manifest definition does not match the assembly reference.

Common causes

Assembly version mismatch

Code compiled against one version of an assembly but a different version is present at run time - common with conflicting transitive dependencies or a missing binding redirect.

Missing dependency in the publish output

A required assembly was not copied to the output/publish folder, so the runtime cannot find it when the type is first used.

How to fix it

Unify the assembly version

Pin a single version of the conflicting package across the solution so compile-time and run-time agree.

.csproj
<PackageReference Include="System.Text.Json" Version="8.0.4" />

Ensure the dependency is in the output

  1. Confirm the package is a direct or properly flowed transitive reference.
  2. Check the publish folder actually contains the assembly DLL.
  3. For tests, ensure the test project references everything the code under test needs.

How to prevent it

  • Use central package management to keep one version of each assembly.
  • Resolve NU1605/NU1107 version conflicts at restore time, before runtime.
  • Verify publish output includes all required assemblies.

Frequently asked questions

What causes ""Could not load file or assembly""?
Code compiled against one version of an assembly but a different version is present at run time - common with conflicting transitive dependencies or a missing binding redirect.
How do I fix "Could not load file or assembly"?
Pin a single version of the conflicting package across the solution so compile-time and run-time agree.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card