89

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?

1
  • Check the .net framework version in class library and the project you are referencing in Aug 20, 2018 at 4:33

24 Answers 24

166

Also, check that the new solution's projects run against a compatible framework to the project you're trying to include. I was trying to include a reference to a 4.0 project in a 3.5 project.

8
  • 3
    Kudos! I was building my solution as a 4.5 and was linking it to a 4.0. Due to our current 4.0 requirement, I downgraded the new solution to 4.0 and now my other project has no problems building against it. +1 for helping me stay sane! Nov 27, 2012 at 22:57
  • 1
    I had overcome so many previous obstacles with this project, was about to throw in the towel when I got this error. You saved it, thanks!
    – Tony D
    Feb 24, 2015 at 21:48
  • 1
    Didn't expect this way the problem would get solved! Cool enuf
    – Irf
    Nov 19, 2016 at 12:10
  • 1
    Then Microsoft should say this and include an option to put them all at the same version.
    – PRMan
    Apr 30, 2017 at 23:12
  • 1
    Saved my day! VisualStudio would not even raise an alert reminding me about that :( May 19, 2017 at 23:52
95

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).

3
  • 1
    Simply restarting VS worked for me. The cause for that problem was a Project/Namespace rename refactoring triggering a bug in R#.
    – mbx
    Apr 10, 2012 at 12:14
  • That worked for me too. I closed all the tabs as you said and then restarted my IDE and voila, I can reference my class library now. Thank you.
    – yuva
    Jan 10, 2019 at 17:20
  • Definitely a continued 'bug' in visual studio 2022. I just encountered this with a new solution under .net core 6. Had me spinning my wheels for about 30 mins. Turned out for me it was a restarting of vs to fix the issue. Very very odd. Jun 9, 2022 at 15:10
33

One possibility is that the target .NET Framework version of the class library is higher than that of the project.

0
8

I had a similar problem, will all my references being buggered up by Resharper - The solution which worked for me is to clear the Resharper Cache and then restarting VS

tools->options->resharper->options-> general-> click the clear caches button and restart VS

0
4

I had a similar problems where VS would sometimes build and sometimes not. After some searching and attempts I discovered that I had an ambiguous reference to a class with the same name in different libraries ('FileManager'). The project that would not build were my Unit Tests that reference all modules in my solution. Enforcing the reference to a specific module sorted things out for me.

My point is: Rather than blaming ReSharper or VS, it may be a good idea to double check if there really isn't some kind of circular reference somehow. More than often, classes with the same names in different modules could cause confusion and is often a symptom of bad design (like in my case).

1
  • +1 in my case two projects with different namespaces were compiled to a .exe with the same name (Assembly name in the Application tab in Visual Studio) - because of a migration project this makes (some) sense; MSBuild from the command line gave us a single not so helpful clue which left us wondering for a while: The type or namespace name 'A' does not exist in the namespace 'B'. Note that no warnings come up for this...
    – Jonno
    Mar 26, 2012 at 8:13
3

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?

1
  • I came across that earlier. And I'm not sure if building it "fixed" the problem. It does compile, but it still shows the classes, using statements, classes, etc. as not being recognized. However, if I purposely break some method call, Visual Studio seems to recognize the error, even if it won't show me the parameter list. I guess it is ReSharpers fault.
    – Brandon
    Feb 9, 2010 at 19:31
3

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.

1
  • 1
    This worked for me, other solutions suggested adding a reference but for some reason I didn't find it until I right clicked the main project in solution explorer.
    – Ralpharama
    Sep 25, 2017 at 10:28
3

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.

1
  • I had the same problem and this seemed to help. Pretty strange.
    – inexcitus
    Oct 16, 2020 at 8:28
2

If you're referencing assemblies for projects that are in the same solution, add a Project reference (using the "Projects" tab) rather than browsing for the dll in the \bin\Debug (or \bin\Release) folder (using the "Browse" tab). See screen shot below. Only browse for the assembly/dll file if it's considered an external assembly.

enter image description here

2

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.

3
  • I did this and got my intelli-sense showing the assembly information again, but as soon as I tried to recompile, my intelli-sense information for that assembly got wiped and the build failed. This is one strange error! Nov 27, 2012 at 22:53
  • I also had to recreate my project from scratch... think I did a few to many uninstall / install in NuGet on a relatively fresh project. May 29, 2015 at 13:10
  • Deleting and recreating references worked for me, full project file delete might be overkill.
    – danjuggler
    Nov 8, 2016 at 19:17
2

I had this problem. It took me ages to figure out. I had people over my shoulder to help. We rebuilt, cleaned and restarted Visual studio and this didn't fix it. We removed and re-added the references...

All to no avail.... Until!

The solution to my problem was that my class declaration was spelt incorrectly.

Before you start judging me harshly, allow me to explain why it wasn't stupid, and also why this mistake could be made by even the most intelligent of programmers.

Since the mistake was early on in the name, it wasn't appearing in the intellisense class listing when I began typing.

e.g.

Class name: Message.cs

Declaration:

public class Massage
{
    //code here

}

At a glance and in a small font, Massage looks identical to Message.

Typing M listed too many classes, so I typed e, which didn't appear in the mistyped version, which gave the impression that the class wasn't being picked up by the compiler.

1
  • So essentially you had a typo. That's not really useful to others.
    – TylerH
    Mar 18, 2018 at 19:39
0

I had a similar issue in VS 2010, when creating a test project for an MVC 2 application. The symptoms were identical.

The message from ReSharper was somewhat misleading. For a moment I completely ignored ReSharper and did it the "manual VS way":

  1. I cleaned the solution.
  2. I manually added the reference to the MVC project.
  3. I manually added the using directives.
  4. ctrl-shift-b

At this stage I got a compilation error: I should have referenced the System.Web.Mvc assembly in my test project (sigh). Adding this reference causes the project to compile. The ReSharper issues remain, but the ReSharper test runner works.

When I restart VS, the ReSharper errors are gone too. I'm not sure if the restart is required - simply closing the .cs file might be enough.

From now on, when I see the ReSharper message

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

I'll read

Failed to reference module. Probably, reference will produce circular dependencies between projects, or you are missing some references to dependencies of the reference's dependencies.

0

Another possible fix that just worked for me:

If you have Assembly A, which references Assembly B, both of which reference a non-project (external) assembly X, and Assembly B's code will not recognize that you have referenced X, then try the following steps in order:

  • Drop reference to X from BOTH A and B
  • Recreate reference to X in B
  • Recreate reference to X in A

Apparently, VS will not recognize a reference to an external assembly in a project that is a dependency of another project that already references the external. By setting up the references again from the ground up, you overcome this. It's just very odd.

1
  • Interesting but true on VS2015. Thanks! Aug 6, 2015 at 11:42
0

I faced this problem, and I solved it by closing visual studio, reopening visual studio, cleaning and rebuilding the solution. This worked for me.

0

If using TFS, performing a Get latest (recursive) doesn't always work. Instead, I force a get latest by clicking Source control => Get specific version then clicking both boxes. This tends to work.

enter image description here

If it still doesn't work then deleting the suo file (usually found in the same place as the solution) forces visual studio to get all the files from the source (and subsequently rebuild the suo file).

If that doesn't work then try closing all your open files and closing Visual studio. When you next open Visual studio it should be fixed. There is a resharper bug that is resolved this way.

0

I had stumbled upon a similar issue recently. I am working in Visual Studio 2015 with Resharper Ultimate 2016.1.2. I was trying to add a new class to my code base while trying to reference a class from another assembly but Resharper would throw an error for that package.

With some help of a co-worker, I figured out that the referenced class existed in the global namespace and wasn't accessible from the new class since it was hidden by another entity of same name that existed in current namespace.

Adding a 'global::' keyword before the required namespace helped me to reference the class I was actually looking for. More details on this can be found on the page listed below:

https://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx

0

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

0

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.

0

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()
    {
        
    }
}

0

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.

0

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.

-1

Unfortunately the only thing that worked for me was completely deleting and recreating the class library project, after having temporarily copied the class files in it elsewhere. Only then would the ASP.Net web project recognise the using statements that referred to the class library project. This was with Visual Studio 2010, not using ReSharper.

-1

I had similar issue. What worked for me is that I had added wrong Class Library from visual studio. I added by using the search feature of visual studio.

What I needed to do was Add New Project > Visual C# > Class Library. And this newly added class library is the right one and can now be added as reference to any project.

-4

You may forget to add reference the class library which you needed to import.

Right click the class library which you want to import in (which contains multiple imported class libraries), -->Add->Reference(Select Projects->Solution->select the class library which you want to import from->OK)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.