Fixing: the type is defined in an assembly that is not referenced error
Getting the type is defined in an assembly that is not referenced error in your .NET application? Check out this post for fixes!
Jan 22, 2023 | Read time 10 minutes🔔 Table of contents
Introduction
Recently I was working on a ASP.NET project, pulled it down from Git and started the build in Visual Studio to be faced with this error:
the type '<type>' is defined in an assembly that is not referenced
This error message appears when a class or type that you are trying to use in your code is not properly referenced in your project. This can happen when the assembly containing the class or type is not included in your project’s references.
Generally, to fix this error, we just need add the reference DLL that contains the missing type to your project!
Some options we can take to fix the “the type ‘
- Add the reference DLL that contains the type to your project. Also check that you are referencing the correct DLL!
- Perform the basic build actions: clean, rebuild, unload/ reload project and restart
- Check the GAC
- Verify the target .NET framework for your project - consider compatibility with .NET standard, .NET core, or .NET framework
- Check your project file *.csproj file
- Check the solution file *.sln
In some cases
1. Add the reference DLL that contains the type to your project.
The first thing to look for when this error comes up is to check that the DLL you have referenced in your project
- Open up your solution in Visual Studio.
- In Solution Explorer, right-click the References or Dependencies node, and then choose either Add Project Reference, Add Shared Project Reference, or Add COM Reference from the context menu.
- Now the references window will open up - just select the correct DLL with your missing type and hit OK
Tip: Make absolutely sure that the DLL contains your type!
Sometimes, in previous projects I thought I have added the correct DLL, however it turned out to be a DLL that contained a old version of the code.
The DLL name was the same, just the new type did not exist and therefore still giving the error!
To show an example of how this problem can arise consider the following Visual Studio solution with 3 projects:
- Project A: Calculator.cs -> this project has reference to Person project
- Project B: Person.cs
- Project C: Console app (Program.cs) that uses Calculator.cs. This project has a reference to Calculator project
Now if we got overloaded constructors in the Calculator.cs class:
public class Calculator
{
public Calculator(string data)
{
}
public Calculator(Person person)
{
}
}
The below will fail with the “the type ‘Person’ is defined in an assembly that is not referenced” error!
internal class Program
{
static void Main(string[] args)
{
var calc = new Calculator("Gary");
}
}
To fix this we need to update our console application with a reference to the Person project
2. Perform the basic build actions: clean, rebuild, unload/ reload project and restart
Sometimes Visual Studio gets itself into a knot and a few actions to reset it can help fix this error!
If you think that you have referenced the correct DLL with the right type, then follow the below steps.
- Open up your solution and go to the Build -> Clean Solution
- Unload and Load you project. Go through each project and identify the ones thats giving this error. Right-click and hit “Unload project”. Then Right-click again and hit “Reload project”
- If the above is not working, just restart Visual Studio to see if it helps
3. Check the GAC
The Global Assembly Cache stores DLLs thats intended to be shared across multiple applications on the computer.
The folder where these DLLs are stored in the Global Assembly Cache is %windir%\Microsoft.NET\assembly
Now this type error: the type '<type>' is defined in an assembly that is not referenced
can come up if your project is referencing the DLL thats in the GAC.
Make sure you right-click you project and view the properties - check to see if its not pointing to a old version of the DLL in the GAC!
4. Verify the target .NET framework for your project
The error of “the type ‘
As an example lets say you have a console app that targets the .NET Framework 4.5 and we reference a class library that targets .NET Framework 4.6.1 - this would fail to build.
You need to have the same (or compatible) target versions in the caller and calling libraries!
Understanding the different .NET versions!
In the begining - roughly around 2002 - Microsoft released the .NET Framework to build applications thats got the goal of being cross-platform.
Write once and run everywhere - the only problem is that its only on Windows platforms! So in 2014, Microsoft started to write .NET Core - which is a ground up rewrite and intends to be truely cross platform.
But now you have the problem of this mess .NET Framework and .NET Core, and adding Mono to the mix aswell. So the .NET Standard was created to keep some form of consistency! Each .NET Standard details the APIs that it supports.
As an example, if you are building a class library for .NET Framework 4.5, .NET Core 2.1, and Mono 4.6, you should target your project to .NET Standard 1.1.
More information on .NET compatibility and the .NET standard can be located here: https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-1-0
6. Check your project file *.csproj file
Sometimes updating a Nuget package can cause this error. The Nuget package manager did not update all projects with the new version and therefore result in conflicting versions!
We can go through the *.csproj file and make sure that the new version is applied.
Additionally in Visual Studio, you can go to Tools->Nuget Package Manager-> Manage Nuget Package for Solutions and go to the Consolidate tab!
7. Check the solution file *.sln for ProjectConfigurationPlatforms
One cause is that we are referencing a library thats built in the wrong Platform/Configuration. For example one project was built with Release|x64
and the others are Debug|Any CPU
To fix this go to the .sln file and locate: ProjectConfigurationPlatforms
- we then change to the appropriate platform/configuration. As an example, in the below, I changed mine to Release|x64
:
{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}.Release|x64.ActiveCfg = Release|x64
{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}.Release|x64.Build.0 = Release|x64
CS0012 System.enum is defined in an assembly not referenced
Sometimes you would think that System.enum is a fundamental thing and should not have this error. The problem usually due to deploying code on a server. The server will need the .NET framework developer installed:
https://learn.microsoft.com/en-US/dotnet/framework/install/guide-for-developers
When developing in Visual Studio, the .NET Framework for developers is already installed and should work on your machine. This is why “it works on my machine” but not the server!
CS0012: The type ‘System.Object’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51’.
The above error came up for me in one project where I was developing a ASP.NET application targeting .NET framework 4.6.1.
To fix this we should update our web.config to reference netstandard
:
<system.web>
<compilation debug="true" targetFramework="4.7.1" >
<assemblies>
<add assembly="netstandard, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51"/>
</assemblies>
</compilation>
<httpRuntime targetFramework="4.7.1" />
Additionally if possible, update the project to .NET Framework 4.7.1 (or even 4.8 as of writing) if you can. Framework versions such as 4.6.1 are not fully compatible with .NET Standard 2.0.
Summary
The error the type '<type>' is defined in an assembly that is not referenced
comes up when we have not referenced the correct project in our Visual Studio solution.
This can be due to referencing the wrong DLL version, incorrect framework targeting, mixing dlls from .NET framework, .NET core and .NET Standard!
We can fix this issue by cleaning and restarting visual studio, clean up the referenced dlls, install the .NET Framework for developers if its missing (usually when deploying to a server!)