66
votes

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.

17

17 Answers

27
votes

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.

22
votes

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.

15
votes

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.

14
votes

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.

6
votes

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

3
votes

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

3
votes

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.

2
votes

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).
1
votes

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!

0
votes

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

0
votes

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.

0
votes

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.

0
votes

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

0
votes

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.

0
votes

@binki's comment helped me;

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

0
votes

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.

0
votes

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 .