102

I know the error message is common and there are plenty of questions on SO about this error, but no solutions have helped me so far, so I decided to ask the question. Difference to most of similar questions is me using App_Code directory.

Error message:

CS0012: The type 'Project.Rights.OperationsProvider' is defined in an
assembly that is not referenced. You must add a reference to assembly
'Project.Rights, version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

Source File:

c:\inetpub\wwwroot\Test\Website\App_Code\Company\Project\BusinessLogic\Manager.cs

Following suggestions here and here, I have deleted all instances of Project.Rights.dll inside C:\Windows\Microsoft.NET/*.* According to this, I checked if .cs files in question have build action set to "Compile". They do. I have also double checked that the .cs file containing the "Project.Rights.OperationsProvider" type is deployed to App_Code directory.

For some reason, application is not looking for the type in the App_Code directory. Since I've deleted all instances of Project.Rights.dll (that I know of), I don't know which assembly the error message is mentioning.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
afaf12
  • 5,163
  • 9
  • 35
  • 58
  • 6
    You're using a class (let's say **A**) that exposes a method/property/something of type Project.Rights.OperationsProvider. Compiler needs to know what's that then it'll search that assembly (Project.Rights). If it doesn't find it (because you don't have a reference to it in your web site project)...you get this error. **Solution:** do NOT remove that assembly from your system!!! ADD a reference to it. – Adriano Repetti Dec 18 '13 at 14:40
  • 3
    Try going to tools - options - projects and solutions - build and run - set both verbosities to Detailed. That will tell you what the dependency is, and where the compiler is looking for it. – danludwig Dec 18 '13 at 14:43

20 Answers20

111

When you get this error it isn't always obvious what is going on, but as the error says - you are missing a reference. Take the following line of code as an example:

MyObjectType a = new MyObjectType("parameter");

It looks simple enough and you probably have referenced "MyObjectType" correctly. But lets say one of the overloads for the "MyObjectType" constructor takes a type that you don't have referenced. For example there is an overload defined as:

public MyObjectType(TypeFromOtherAssembly parameter) {
    // ... normal constructor code ...
}

That is at least one case where you will get this error. So, look for this type of pattern where you have referenced the type but not all the types of the properties or method parameters that are possible for functions being called on that type.

Hopefully this at least gets you going in the right direction!

drew_w
  • 10,320
  • 4
  • 28
  • 49
  • 19
    Note about extension methods: if two classes have extension methods with the same name, parameters from one type can "infect" usage of extension method from another type. – Athari Aug 16 '14 at 22:13
  • @Athari thank you, I never would have figured that out – Dave Cousineau Jan 30 '18 at 20:46
  • 1
    Just chanced upon this answer after a while of looking. This is exactly my problem and your explanation was very helpful. Thanks loads. – Diane Aug 23 '18 at 15:06
  • 4
    But what if I don't want to use the constructor with the reference? I want to use the other ones, without adding a reference. It seems like this should be possible. – Robert Iagar Jan 31 '19 at 08:20
  • 1
    This seems really odd, but I got this error because I had a mismatch in the case of one of the assembly names (looks like nuget was case sensitive?)! I had a library C reference libraries B and A. Library B also referenced library A but packaged A as a dependency using the name 'a' rather than 'A' . Building library C, I kept getting this error until I corrected the name of the dependency in B! – Tolu May 24 '19 at 23:40
  • @Athari thanks for sharing that. I've just had this exact issue here. Two different extension methods with same name in different assemblies, the first one (which I was using) had 3 mandatory arguments and 2 optional ones (which I wasn't passing), and the other extension method on the other assembly had 4 mandatory arguments. As soon as I started passing the 4th argument immediately this second extension method (with the unknown type) started being tested (to find the best extension) and it broke the build. To sum I had to add the reference to the unknown type even without using it. – drizin Sep 30 '22 at 20:39
62

Check target framework in the projects.

In my case "You must add a reference to assembly" actually meant, that caller and reference projects didn't have the same target framework. The caller project had .Net 4.5 , but referenced library had target 4.6.1.

I am sure, that MS compiler can be smarter and log more meaningful error message. I've added a suggestion to https://github.com/dotnet/roslyn/issues/14756

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
  • Also make sure you know the difference between framework types. They have similar slightly confusing names beginning with .NET, and close version numbers, but they are incompatible. https://learn.microsoft.com/en-us/dotnet/standard/choosing-core-framework-server – Cale Sweeney Oct 19 '21 at 18:28
16

In my case this was because doing a NuGet package update had only updated references to a dll dependency in some but not all projects in my solution - resulting in conflicting versions. Using a grep-style tool to search text within *.csproj files in my solution it was then easy to see the projects that still needed to be updated.

rogersillito
  • 869
  • 1
  • 10
  • 27
  • Thanks for this answer - I figured it was something close to this but was glad to see it. Put another way - my parent project (service) calling a constructor with an optional parameter in my data layer had not had that reference added to the .csproj. Comparing the child and parent .csproj files should illuminate an ItemGroup that should be in the parent that's not. – Ryanman Oct 23 '17 at 20:10
  • 4
    Visual Studio Package Manager window for the Solution ( not for individual project) has **Consolidate** tab, that shows which packages have different versions in different projects. It provably better than grep-like tool – Michael Freidgeim Jan 27 '18 at 22:50
11

When you get this error, it means that code you are using makes a reference to a type that is in an assembly, but the assembly is not part of your project so it can't use it.

Deleting Project.Rights.dll is the opposite of what you want. You need to make sure your project can reference the assembly. So it must either be placed in the Global Assembly Cache or your web application's ~/Bin directory.

Edit-If you don't want to use the assembly, then deleting it is not the proper solution either. Instead, you must remove all references to it in your code. Since the assembly isn't directly needed by code you've written, but instead by something else you're referencing, you'll have to replace that referenced assembly with something that doesn't have Project.Rights.dll as a dependency.

mason
  • 31,774
  • 10
  • 77
  • 121
  • 2
    I can't use the assembly, that is a requirement. I need to get rid of it and store the class inside App_Code directory. I know how it sounds, believe me. Being tasked to alter an application so all business logic is stored inside App_Code instead of DLLs... is not fun. – afaf12 Dec 18 '13 at 14:42
  • 1
    No, it means he's using something with a reference to it (not that he's using directly). @afaf12 if you have to get rid of it...you have to check **who** is using it (imagine this as an _indirect_ reference). You can't simply paste code in App_Code directory, any compiled assembly will still reference original (and external) one... – Adriano Repetti Dec 18 '13 at 14:44
  • I had a similar and your post helped a lot. In my case i had an assembly "A" referenced. This assembly was using another assembly "B". I kept getting an error that assembly "B" is missing though i added it to my project. Once i copied assembly "B" to my bin directory the problem was solved. The reason is that my project wasn't using assembly "B" but assembly "A" was using and it didn't found it and thus throw an exception. Thanks. – ykh Mar 07 '16 at 19:53
4

In my case, I was referencing a library that was being built to the wrong Platform/Configuration (I had just created the referenced library).

Furthermore, I was unable to fix the problem in Visual Studio Configuration Manager -- unable to switch and create new Platforms and Configurations for this library. I fixed it by correcting the entries in the ProjectConfigurationPlatforms section of the .sln file for that project. All its permutations were set to Debug|Any CPU (I'm not sure how I did that). I overwrote the entries for the broken project with the ones for a working project and changed the GUID for each entry.

Entries for functioning project

{9E93345C-7A51-4E9A-ACB0-DAAB8F1A1267}.Release|x64.ActiveCfg = Release|x64 {9E93345C-7A51-4E9A-ACB0-DAAB8F1A1267}.Release|x64.Build.0 = Release|x64

Entries for corrupted project

{94562215-903C-47F3-BF64-8B90EF43FD27}.Release|x64.ActiveCfg = Debug|Any CPU {94562215-903C-47F3-BF64-8B90EF43FD27}.Release|x64.Build.0 = Debug|Any CPU

Corrupted entries now fixed

{94562215-903C-47F3-BF64-8B90EF43FD27}.Release|x64.ActiveCfg = Release|x64 {94562215-903C-47F3-BF64-8B90EF43FD27}.Release|x64.Build.0 = Release|x64

I hope this helps someone.

Joseph
  • 903
  • 1
  • 10
  • 25
  • For me it was similar. VIsual Studio have changed GUIDs for some of my projects from `FAE04EC0-301F-11D3-BF4B-00C04F79EFBC` (C#) to `9A19103F-16F7-4668-BE54-9A1E7A4F7556` (ASP.NET). After I changed them back everything gone well. – scor4er Mar 20 '20 at 13:44
3

It just happened to me that different projects were referencing different copies of the same dll. I made sure all referenced the same file on disk, and the error disappeared as I expected.

Gonzalo Méndez
  • 548
  • 7
  • 18
2

Unloading and reloading the class library in Visual Studio solved this for me.

Pad
  • 164
  • 4
1

For me, this was caused by the project both directly and indirectly (through another dependency) referencing two different builds of Bouncy Castle that had different assembly names. One of the Bouncy Castle builds was the NuGet package, the other one was a debug build of the source downloaded from GitHub. Both were nominally version 1.8.1, but the project settings of the GitHub code set the assembly name to BouncyCastle whereas the NuGet package had the assembly name BouncyCastle.Crypto. Changing the project settings, thus aligning the assembly names, fixed the problem.

mathiash
  • 93
  • 1
  • 5
1

It didn't work for me when I've tried to add the reference from the .NET Assemblies tab. It worked, though, when I've added the reference with BROWSE to C:\Windows\Microsoft.NET\Framework\v4.0.30319

Cătălin Rădoi
  • 1,804
  • 23
  • 43
0

one of main reason can be the property of DLL you must before do any thing to check the specific version property if it true make it false

Reason: maybe the source code joined with other (old)version when you build it , but this Library upgraded with new update the version now different in the Assembly Cash and your application forbidden to get new DLL ,and after disable specific version property your applacaten will be free to get the new version of DLL references

0

Maybe a library (DLL file) you are using requires another library. In my case, I referenced a library that contained a database entity model - but I forgot to reference the entity framework library.

Graham Laight
  • 4,700
  • 3
  • 29
  • 28
0

This can also mean you use a library, which exposes (public) types that are defined in a library. Even when you do not use these specifically in your library (the one that doesn't build).

What this probably prevents is you writing code that uses a class (which in its signature has the types from a library not referenced) that you cannot use.

Aage
  • 5,932
  • 2
  • 32
  • 57
0

For me the reason why the error appeared was that the WebForm where the error was reported has been moved from another folder, but the name of its codefile class remained unchanged and didn't correspond to the actual path.

Initial state:
Original file path: /Folder1/Subfolder1/MyWebForm.aspx.cs
Original codefile class name: Folder1_Subfolder1_MyWebForm

After the file was moved:
File path: /Folder1/MyWebForm.aspx.cs
Codefile class name (unchanged, with the error shown): Folder1_Subfolder1_MyWebForm

The solution:
Rename your codefile class Folder1_Subfolder1_MyWebForm
to one corresponding with the new path: Folder1_MyWebForm

All at once - problem solved, no errors reporting..

0

In my case the version of the dll referenced was actually newer than the one that I had before.

I just needed to roll back to the previous release and that fixed it.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Hugo Nava Kopp
  • 2,906
  • 2
  • 23
  • 41
0

The type 'Domain.tblUser' is defined in an assembly that is not referenced. You must add a reference to assembly 'Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

**Solved:**
 Add reference of my domain library layer to my web app libary layer

Note: Make sure your references are correct according to you DI container

Asif Raza
  • 971
  • 6
  • 14
0

In my case this was because I used

Implicit Operator

between BLL and DAL classes.when I want to use BLL Layer In Application Layer I got this error. I changed

implicit operator

to

explicit operator

it be OK. Thanks

Amirhossein Yari
  • 2,054
  • 3
  • 26
  • 38
0

I have a similar problem, and I remove the RuntimeFrameworkVersion, and the problem was fixed.

Try to remove 1.1.1 or

Arash Yazdani
  • 302
  • 2
  • 12
0

My problem was that the Output Type for one of my projects was set to Console Application. To fix this, I right-clicked the project, chose Properties, clicked the Application tab, and change Output Type (from Console Application) to Class Library. After I re-compiled, this error went away.

0

For VS 2022. Closing the VS and reopening it worked for me.

tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83
-3

Clean your solution and rebuild worked for me (in Visual Studio, these are options you get when you right click in your solution explorer), the error is gone in my project.

Joost
  • 11