System.TypeLoadException
A TypeLoadException is thrown when the runtime finds an assembly but cannot load a type from it. The message says which type and which assembly, and gives the reason, such as because the parent type is an interface or because the method has no implementation.
In nearly every case the cause is a mix of versions. The code was compiled against one version of a library, and another version is in the folder when it runs.
Minimum version: >= 1.1 >= Core 1.0
Statistics
Common causes
The type you ask for does not look the same at run time as at build time. These are the usual reasons.
Different versions of an assembly at build and run time
Your code was built against a library where a class was an abstract class, a method was virtual or an interface had certain members. The version that is deployed changed it. The compiler is happy with the old version, and the runtime fails when it loads your type.
Deploy exactly the assemblies that the application was built with. Do a clean build, delete bin and obj, and use the output of dotnet publish. If your own code is built against the old version, rebuild it against the new one.
Old files left in the deployment folder
Copying a new build over an old one keeps files that are no longer part of the project. A library that is gone from the project, but still in the folder, can be loaded instead of the right one.
Deploy to an empty folder, or use a deployment method that removes the files that are no longer part of the build, such as a zip deploy or a container image.
Packages that need different versions of a dependency
Two packages require different versions of the same library, and only one version ends up in the output. The package that expected the other version fails when it loads a type that changed.
List the versions that resolve, and pick one that all of them work with, either by upgrading the packages or by pinning the version:
dotnet list package --include-transitive
Plugins and dynamically loaded assemblies
When you load assemblies with Assembly.LoadFrom, or an add-in system, the same assembly can be loaded twice from two folders, and a type from one copy is not the same as the one in the other.
Load plugins in an AssemblyLoadContext that shares the contracts with the host, and keep the contract assembly in one place.
How to fix it and prevent it
Rebuild everything against the same versions
Do a clean restore and build of the solution, so every project uses the same version of a shared library. Check for a mix in the dependencies of the projects with the command above.
Compare the deployed files to the publish output
Look at the version of the assembly named in the message, on the machine where it fails. The message includes it, and so does the file properties. If the version is not what you built, the deployment is wrong.
Console.WriteLine(typeof(object).Assembly.GetName().Version);
Use central package management
In a solution with many projects, put the versions of packages in Directory.Packages.props. Every project then uses the same version, and the mix stops being possible.
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>
How to read the stack trace
The message is more useful than the stack trace, which is often short:
System.TypeLoadException: Could not load type 'CardProvider' from assembly 'Shop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the parent type is an interface.
at Program.<Main>$(String[] args)
The type that failed to load is CardProvider, in the assembly Shop, and the reason is the parent type is an interface. The class was compiled against a library where its base type was an abstract class, but the library that is deployed has an interface with that name. Find the base type of the class, and look at which version of that library was deployed.
Should you catch it?
No. It means that the deployment is broken, and the application cannot work as intended. Fix the files, and let the exception end the request so it is logged.
It can make sense for a plugin system, where one plugin that cannot be loaded should not stop the others. Catch it around the loading of each plugin, and log the type and assembly from the message.
Find TypeLoadException before your users do
elmah.io logs every unhandled exception in your .NET application with its stack trace and request details, groups identical errors and notifies you when a new one appears.
Start free trialRelated exceptions
- FileNotFoundException is thrown when an assembly is missing, and not just the wrong version.
- TypeInitializationException is thrown when the type loads but its static initializer fails.
- InvalidCastException can be a symptom of the same type being loaded twice from two places.
Frequently asked questions
What is the difference between TypeLoadException and FileNotFoundException?
A FileNotFoundException says that the assembly was not found. A TypeLoadException says that it was found, but the type in it cannot be used the way the code expects.
Why does it work on my machine?
Your machine has the versions that the code was built with. The server has other files, or older ones, left from an earlier deployment.
Do I need binding redirects?
On .NET Framework, a binding redirect makes the runtime use one version for requests for another. On current .NET, the version that is in the output is used, so check what the build copies.
How can I see which assembly version was loaded?
Log typeof(SomeType).Assembly.GetName().Version and the location at startup. For a running process, a tool like Process Explorer lists the loaded modules and their versions.
Let your AI agent track it down
Connect Claude Code, Cursor, VS Code or Visual Studio to the elmah.io MCP server and ask your agent to look into TypeLoadException for you. For example:
The agent reads the stack trace and request details from elmah.io, finds the code in your repository and proposes a fix. In Claude Code, add the server with one command:
claude mcp add --transport http --client-id claudecode elmahio https://mcp.elmah.io/mcp
The MCP server is included on every plan and is currently in beta. Set up the MCP server.
Further reading
YouTube videos
Answers from Stack Overflow
NOTE - If this answer doesn't help you, please take the time to scroll down through the other answers that people have added since.
Really short answer
Delete all your bin and obj directories and rebuild everything. The versions of the dlls don't match.
Short answer
This can happen if you add a method to an interface in one assembly, and then to an implementing class in another assembly, but you rebuild the implementing assembly without referencing the new version of the interface assembly.
In this case, DummyItem implements an interface from another assembly. The SetShort method was recently added to both the interface and the DummyItem - but the assembly containing DummyItem was rebuilt referencing the previous version of the interface assembly. So the SetShort method is effectively there, but without the magic sauce linking it to the equivalent method in the interface.
Long answer
If you want to try reproducing this, try the following:
Create a class library project: InterfaceDef, add just one class, and build:
public interface IInterface { string GetString(string key); //short GetShort(string key); }Create a second class library project: Implementation (with separate solution), copy InterfaceDef.dll into project directory and add as file reference, add just one class, and build:
public class ImplementingClass : IInterface { #region IInterface Members public string GetString(string key) { return "hello world"; } //public short GetShort(string key) //{ // return 1; //} #endregion }Create a third, console project: ClientCode, copy the two dlls into the project directory, add file references, and add the following code into the Main method:
IInterface test = new ImplementingClass(); string s = test.GetString("dummykey"); Console.WriteLine(s); Console.ReadKey();Run the code once, the console says "hello world"
Uncomment the code in the two dll projects and rebuild - copy the two dlls back into the ClientCode project, rebuild and try running again. TypeLoadException occurs when trying to instantiate the ImplementingClass.
By Benjol. Read the original answer on Stack Overflow.
You can use JetBrains dotPeek (free), Reflector, ILDASM or ILSpy to get the assembly version.
You usually can find ILDASM in C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\ildasm.exe (where v8.1A is the version of the Windows SDK installed).
ILDASM:

Reflector:

By Patrick Hofman. Read the original answer on Stack Overflow.
Important: This will occur when a service or dependency can't be resolved. Run your code in debug mode or you can see what is missing using -verbose with your migration command. If you can resolve dependencies and run the code without errors, you can proceed with the migrations. However, if you can't do that or want to run migrations separately from the app logic, please follow the instructions.
EF calls CreateWebHostBuilder or BuildWebHost without running Main. So Iconfiguration is null.
Create a new class which inherited from IDesignTimeDbContextFactory.
public class YourDbContext : DbContext
{
//Dbcontext implementation
}
public class YourDbContextFactory : IDesignTimeDbContextFactory<YourDbContext>
{
public YourDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<YourDbContext>();
optionsBuilder.UseSqlServer("your connection string");
return new YourDbContext(optionsBuilder.Options);
}
}
You are using a new .net core EF which uses IHostBuilder.(in an older version like yours the provider is IWebHostBuilder).
The tools first try to obtain the service provider by invoking the Program.CreateHostBuilder(), calling Build(), then accessing the Services property.
You can learn more about Design-time DbContext Creation from Here
It may happen from a condition in your startup file or while you are injecting. for example, you have a flag that checks if some variable in appsettings is true to use inmemory database instance.
EF needs to build the model and use the DbContext without starting the application. When EF invokes methods, your config services are still null that's why you get an error.
Make sure you have installed the package
Microsoft.EntityFrameworkCore.Tools
By Shervin Ivari. Read the original answer on Stack Overflow.
There is a couple of ways to do it:
If you reference the dll in Visual Studio right click it (in ProjectName/References folder) and select "Properties" you have "Version" and "Runtime Version" there.
Alternatively, investigate it in code:
Assembly assembly = Assembly.LoadFrom("TestAssembly.dll"); Version ver = assembly.GetName().Version;
By PiotrWolkowski. Read the original answer on Stack Overflow.
If you know Class, that belongs to assembly, you can use GetTypeInfo
var runtimeVersion = typeof(MyClass)
.GetTypeInfo()
.Assembly
.GetCustomAttribute<AssemblyFileVersionAttribute>();
var version = runtimeVersion.Version;
The example is for .Net Core from https://developers.de/blogs/damir_dobric/archive/2017/06/27/how-to-deal-with-assembly-version-in-net-core.aspx
By Michael Freidgeim. Read the original answer on Stack Overflow.
Source: Stack Overflow