The type is defined in an assembly that is not ref

2019-01-02 18:11发布

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.

17条回答
梦寄多情
2楼-- · 2019-01-02 18:53

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

查看更多
查无此人
3楼-- · 2019-01-02 18:53

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

查看更多
只若初见
4楼-- · 2019-01-02 18:55

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

查看更多
姐姐魅力值爆表
5楼-- · 2019-01-02 18:56

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.

查看更多
余生请多指教
6楼-- · 2019-01-02 18:57

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.

查看更多
皆成旧梦
7楼-- · 2019-01-02 18:57

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.

查看更多
登录 后发表回答