Error: Reference to type claims it is defined, but it could not be found

C#Visual StudioAssemblies

C# Problem Overview


I have a solution with 3 projects:

  • ParsersBase, that define an interface IParseRule
  • ParsersLibrary, that have a reference to ParsersBase and define a class HtmlImageUrlParseRule : IParseRule
  • ParsersLibraryTest, that have a reference to ParsersBase and ParsersLibrary and define a test class with some test methods

When I'm trying to build it, I get a warning:

> Reference to type 'AVSoft.ParsersBase.IParseRule' claims it is defined in 'c:\Users\Tim\Dropbox\projects\Image Downloader\ParsersLibrary\bin\Debug\ParsersLibrary.dll', but it could not be found

Why VS is trying to find AVSoft.ParsersBase.IParseRule in ParsersLibrary.dll? ParsersLibraryTest has a reference to ParsersBase, it just doesn't make any sense.

C# Solutions


Solution 1 - C#

This error seems to cover a variety of scenarios. In my case, closing and re-opening Visual Studio was the trick. After restarting Visual Studio, I was able to build the project as expected.

Solution 2 - C#

Another way this could happen is if you're using several NuGet packages where one, probably central, package has been updated but some additional functionality packages haven't been.

To work through my recent example - the error was "Reference to type 'ConsumerSubscriptionConfigurator<>' claims it is defined in 'MassTransit', but it could not be found". This is because we had updated MassTransit from 2 to 3, but we had not updated some of the other optional packages (MassTransit.log4net and MassTransit.Autofac) to the same version number. It appears as if assembly redirection had kept everything working until we tried to use one more additional feature.

Solution 3 - C#

I had a similar problem. The site was running a cached version of the dll and not the one I had deployed to the bin directory. I cleared the temporary asp.net folder contents and this solved the issue.

Solution 4 - C#

It was my fault, I had a ParsersLibrary project at the start and then renamed it to ParsersBase, but I didn't rename an assembly name, then I added a ParsersLibrary project again.

So, two projects had the same assembly name and it's not very good, is it? :) Assemblies overlap each other, so I have this error.

Solution 5 - C#

I had the similar problem: Reference to type 'Func<>' claims it is defined in 'mscorlib', but it could not be found. The problem was following: my solution had Target Framework = 3.5 and I added a reference to Microsoft.Practices.Prism v 4.0 which is built against the framework 4.0.

After changing target framework to 4.0 it worked

Solution 6 - C#

I hit this exception today. The problem in my case was I had some.package v2.1 installed in my host and some.package v2.3 installed in other projects. Update-Package on the host project to v2.3 fixed the issue.

Solution 7 - C#

I my case, I tried to test a WPF project with a .NET Core (3.1) test project which could not reference the needed WindowsBase.dll.

Solution 8 - C#

It looks like things are a bit easier now than they were before.

As other answer(s) have basically already stated, this error can result from an older version of the same NuGet package not having some of the newer types in it. While in production, this is generally managed through proper versioning, in development, you may end up reusing the same version number when making changes. And that's a likely place where this problem can arise.

To fix this, you can often just clear the cache by doing the following:

  1. In Visual Studio, go to Tools > NuGet Package Manager > Package Manager Settings.
  2. In the pop-up menu, navigate to NuGet Package Manager > General.
  3. In the options on the right, click Clear All NuGet Cache(s).

Solution 9 - C#

Updating/consolidating packages didn't help. Even a clean repo and a restart of Visual Studio didn't solve it for for me.

But rebooting did fix the problem!

Solution 10 - C#

@binki's comment helped me;

> deleting all .vs, bin, and obj folders, and then reopening the project

Solution 11 - C#

ParsersLibraryTest needs to reference ParsersBase. The second part of the error should read "You must add a reference to assembly 'ParsersBase..."

Solution 12 - C#

I tried all of the above answers but none resolved my issue.

In the end, I checked in my latest code (GIT), then recloned the repository in a different location.

Not ideal, but at least problem solved.

Solution 13 - C#

I had the similar problem: Reference to type 'Func<>' claims it is defined in 'mscorlib', but it could not be found. I have a lib of .Net 4 that was referenced by a .Net 3.5 program. After upgrading both to 4.61 it worked.

Seems like Func<T> is missing in .Net 3.5 and just upgrading that sample app would have been enough.

Further exp: Someone had added a signature in the library project (.Net 4) that uses a Func<T> parameter. But the sample program (3.5) already existed and ran fine so far. But at time of recompilation a dependency of a dependency clashed. Because mscorelib-3.5 had been already loaded. All happens at compilation time.

Solution 14 - C#

The only way I could overcome this error was to force uninstall of all nuget packages related and then reinstalling them. Sad but true.

Solution 15 - C#

I've just struggled with this error for a while now and finally get around it. This is how to re-produce it and how I fixed it.

The problem was: The packages were referenced by Right clicked -> add refernece -> Browse (choose). Then were added again as NuGet packages.

The solution was:

  • Remove the added references.
  • Remove the installed packages from .csproj.
  • Re-install the required packages from NuGet package mangager.
  • Close Visual Studio and re-open it.
  • Clean Project.
  • Build Project.

Note: If you couldn't remove the referenced files (no Remove option on right click) try close Visual studio and re-open it. Or delete or move the dll that were referenced then try again.

Solution 16 - C#

For me, I had chosen incorrect project, I was creating a class library project, I had to chose "Class Library (.Net framework)" but I had chosen "Class Library (.Net standard)"

Replacing the same resolved the issue.

Solution 17 - C#

The problem was following: my solution had Target Framework = 3.1 and I added a reference to Microsoft.EntityFrameworkCore.SqlServer v 2.0 which is built against the framework .

Solution 18 - C#

I had this problem with one of my library projects inside of a solution, after I switched from .NET Framework to .NET Standard. Eventually I just removed the project reference and added it again inside the application project that was reporting the problem. Oddly enough, the only thing that changed was project GUID switching to lower case from the previous upper case.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTim RudnevskyView Question on Stackoverflow
Solution 1 - C#Scott SimontisView Answer on Stackoverflow
Solution 2 - C#Rob ChurchView Answer on Stackoverflow
Solution 3 - C#wiseowl2828View Answer on Stackoverflow
Solution 4 - C#Tim RudnevskyView Answer on Stackoverflow
Solution 5 - C#Alexandra ShView Answer on Stackoverflow
Solution 6 - C#Chris MarisicView Answer on Stackoverflow
Solution 7 - C#IngoBView Answer on Stackoverflow
Solution 8 - C#PanzercrisisView Answer on Stackoverflow
Solution 9 - C#CDichterView Answer on Stackoverflow
Solution 10 - C#JDandChipsView Answer on Stackoverflow
Solution 11 - C#SlugartView Answer on Stackoverflow
Solution 12 - C#Kyle BarnesView Answer on Stackoverflow
Solution 13 - C#RobettoView Answer on Stackoverflow
Solution 14 - C#BornToCodeView Answer on Stackoverflow
Solution 15 - C#Wael AlshabaniView Answer on Stackoverflow
Solution 16 - C#JayView Answer on Stackoverflow
Solution 17 - C#jayanga View Answer on Stackoverflow
Solution 18 - C#jahuView Answer on Stackoverflow