Visual Studio asks me to reference a nonexistent assembly
Asked Answered
T

3

14

I somehow brought my project to a state where Visual Studio 2013 fails to compile it with a ridiculous error:

The type 'System.Collections.Generic.Dictionary`2' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

First of all, there is no such assembly, it does not exist. Second, Dictionary<TKey, TValue> is defined in mscorlib.dll, which is referenced by default, of course. Resharper (which has its own code analysis engine) reports that the solution should compile normally.

I don't know how on Earth it could happen, because my latest changes have nothing to do with the supposedly erroneous place at all. The line references some standard LINQ functions (GroupBy and ToDictionary), and it worked for months without any changes. Unfortunately, I cannot create any MREs: obviously, this error only appears in the context of my huge solution, and only with some specific changes made in the presumably irrelevant places.

Here's what I tried, and it didn't work:

  • Clean + Rebuild
  • Changing target Framework from .NET 4.5 to .NET 4.5.1 or .NET 4.0

Has anyone seen such a quirk before?

Tripos answered 16/1, 2015 at 1:48 Comment(2)
Is this a portable library? Perhaps the stubs got broken somehow.Backlash
@Backlash No, this is a regular class library, targeting .NET 4.5.Tripos
B
2

Check that all the (core .net) references in your project are valid and they all reference the same .NET framework version.

If in doubt, remove all references and add them again (using the 4.0.0.0) versions.

I had this happen to me once (with the exact same error) and it was due to some "screw up" on the version of the references. I never got to find out WHY it happened, but removing all references and adding them again worked for me.

Bannock answered 16/1, 2015 at 7:13 Comment(2)
This magic rewrote half of my .csproj file, and the project now builds :) Thank you! I wish I knew what happened there in the internals of MSBuild...Tripos
Removing all references sounds too scary for me... Is there a way to print out the reference dependencies before removing them all?Hootenanny
B
19

I had the same problem, but it was on a website project - not any easy way to remove & re-add the references. Luckily, I found that this answer worked for me - and was even quicker than messing with references https://mcmap.net/q/758789/-assembly-not-referenced-compilation-error-in-foreach-loop-in-razor-view

adding a new assembly tag to web.config seems to have resolved this issue. The tag I added was under the tag and as follows:

<assemblies>
     <add assembly="System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
</assemblies>
Bounded answered 5/6, 2015 at 0:2 Comment(0)
P
4

I had the exact same issue and it seemed to be related to two NuGet packages containing the same assembly "System.Collections.Immutable". The two NuGet packages where "Microsoft.Bcl.Immutable" (Unlisted) and "System.Collections.Immutable".

I haven't been able to reproduce the issue reliably, and it seems the problem was solved by using VS2015.

However, i would suggest looking for NuGet packages containing the same assembly.

Removing Microsoft.Bcl.Immutables solved this for me.

Protease answered 2/3, 2017 at 15:2 Comment(2)
Thank you, had the same issue and it only appeared when i tried to publish my site, local was fineHodge
@PaulBecker Glad to help! :)Protease
B
2

Check that all the (core .net) references in your project are valid and they all reference the same .NET framework version.

If in doubt, remove all references and add them again (using the 4.0.0.0) versions.

I had this happen to me once (with the exact same error) and it was due to some "screw up" on the version of the references. I never got to find out WHY it happened, but removing all references and adding them again worked for me.

Bannock answered 16/1, 2015 at 7:13 Comment(2)
This magic rewrote half of my .csproj file, and the project now builds :) Thank you! I wish I knew what happened there in the internals of MSBuild...Tripos
Removing all references sounds too scary for me... Is there a way to print out the reference dependencies before removing them all?Hootenanny

© 2022 - 2024 — McMap. All rights reserved.