Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type is defined in an assembly that is not referenced, how to find the cause?

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.

like image 866
afaf12 Avatar asked Dec 18 '13 14:12

afaf12


People also ask

Is defined in assembly that is not referenced?

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.

How do I fix error CS0012?

You could resolve this CS0012 by compiling with /reference:cs0012b. dll;cs0012a. dll , or in Visual Studio by using the Add Reference Dialog Box to add a reference to cs0012a. dll in addition to cs0012b.

How do I add a reference to .NET assembly?

To use the Add Reference window In the AOT, right-click References, and then click Add reference. The Add reference window opens. Review the top list to locate the assembly that contains the control you want to use. Click the assembly name and then click the Select button.


1 Answers

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!

like image 112
drew_w Avatar answered Oct 05 '22 19:10

drew_w