Why can't I reference my class library?

Viewed 133465

I have a solution that contains a website and a class library in Visual Studio 2008.

I then have another web site project outside of the solution that needs to reference the class library. I right click the Bin folder or Project and select Add Reference, then select my Class Library Project, it adds the 15 or so DLLs that the class library needs to the websites bin folder, but none of the .cs files recognize the using statements.

using MyLibrary.MyFolder;

It says that it can't resolve it, and ReSharper just says it can be safely removed since it's not being used.

ReSharper can recognize that it needs the reference and suggests that it "Reference MyLibrary and use MyFolder". I'm not sure why it's suggesting I add a reference I already have. When I go with the suggestion, I get the error

"Failed to reference module. Probably, reference will produce circular dependencies between projects."

I've tried going to the websites property pages and removing all the references and re-adding them, but it gives the same errors. Any ideas why this isn't working?

24 Answers

I found how to fix this issue (for me at least). Why it worked, I'm not sure, but it did. (I just tried against a second website that was having the same problem and the following solution worked for that as well).

I tried the normal cleaning of the projects and rebuilding, shutting down all my Visual Studio instances and restarting them, even tried restarting my computer.

What actually worked was opening up the project in Visual Studio, closing all the open tabs, and then shutting it down.

Before I had left the tabs open because I didn't think it mattered (and I hardly ever close the tabs I'm using).

Since they are both in the same solution, instead of adding a reference to the DLL, add a reference to the class library project itself (the Add Reference dialog will have a tab for this).

Ahh, it's a different solution. Missed that. How about you try instead of adding a reference to the project addding a reference to the compiled DLL of your class library. The Add Reference dialog has a Browse tab which does this.

This sounds like a similar issue with ReSharper:

http://www.jetbrains.net/devnet/thread/275827

According to one user in the thread forcing a build fixes the issue (CTRL+Shift+B) after the first build..

Sounds like an issue with ReSharper specifically in their case.. Have you tried building regardless of the warnings and possible false errors?

After confirming the same version of asp.net was being used. I removed the project. cleaned the solution and re-added the project. this is what worked for me.

I deleted *.csproj.user ( resharper file) of my project, then, close all tabs and reopen it. After that I was able to compile my project and there was no resharper warnings.

If both projects are contained within the same solution, it will be more apropiate if you add the reference for the project you need, not its compiled dll.

enter image description here

I had the exact same problem.

I tried closing Visual Studio numerous times, I tried deleting and adding a new class library.Checked if I had the right version, if I had referenced it in the target project. Nothing worked.

Then I thought maybe, just maybe I cannot reference the library because it was empty... and that was it.

As soon as I added a class to it the problem was fixed. So if you have tried everything and you are close to losing your sanity. Just try adding something to the class library.

the solution for was just adding the access modifier my class didnt have any access modifier then i just added public and it worked! enter image description here Contracts class library:

namespace Contracts
{
    public interface ILoggerManager
    {
        void LogInfo(string message);
        void LogWarn(string message);
        void LogDebug(string message);
        void LogError(string message);
    }
}

Logger service class library:

using Contracts;
using NLog;
public class LoggerManager  : ILoggerManager
{
    private static NLog.ILogger logger = LogManager.GetCurrentClassLogger();
    public LoggerManager()
    {
        
    }
}

I tried various solutions for this issue. An old WebForms application refused to acknowledge the existence of a library even though a reference existed.

Oddly, what worked was to add and reference the class library in Visual Studio 2022, safe the project, then reopen in an earlier version of Visual Studio.

My solution was simple, but I'll share it in case someone else has the same issue and finds this question by googling like I did.

It turns out that the most recent build of the supporting DLL was done in Debug mode, and my code was looking at the Release version of the DLL. I rebuilt the DLL in Release mode and all is working properly.

Related