Visual Studio 2015 - Adding Reference to class library C# ASP.NET - c#

I am trying to add references to class libraries under my Web Forms solution. I added several class libraries to the main solution (via File -> New Project -> Add to solution). When I tried to add references to them (both from the solution explorer window and the File menu), I received a popup stating simply: "Cannot add reference to ," with no additional error messages.
I verified that all projects are using the same version of the .NET Framework (4.5, NOT the client version) and the same target architecture (x64), as I have seen in other Stack Overflow posts.
I did try and add the reference via browse, and added the DLLs from the /bin/debug folder, and that actually worked. The namespaces were picked up and even shown in intellisense. My concern here is breaking these references during release builds and deployments.

I found a workaround for this problem.
When you head over to your solution and add a new project, you'll automatically be directed in the Web category. Instead head over to the Windows tab and pick that Class Library instead.

Related

Type or namespace name could not be found for a DLL in the bin folder

I have a third-party DLL. I have added this DLL to the bin folder of a Web Site project and can access it no problem. I then added it to the bin folder of a Web Application project, and when trying to access it I receive "The type or namespace name could not be found."
From my research, it could be an issue with .NET target version. The Web Application project is targeting a higher version of .NET than the Web Site project so this should not be an issue.
I also checked that the file is present in the bin folder in the file system and has the same permissions as other files in the bin folder.
I have been searching for a solution for a while but most things that come up are for other project types like WPF, or talk about project references but this is a DLL.
Anything else I can check as to why the DLL cannot be used by the Web Application project? I am using Visual Studio 2017.
Managing references in a project:
Before you write code against an external component or connected service, your project must first contain a reference to it. A reference is essentially an entry in a project file that contains the information that Visual Studio needs to locate the component or the service.
To add a reference, right click on the References or Dependencies node in Solution Explorer and choose Add Reference. You can also right-click on the project node and select Add > Reference. For more information, see How to: Add or remove references.
Web Site projects are an odd old beast which are different from every other project type. They're also no longer recommended
For new development, we recommend that you choose web application projects. This topic explains that web site projects have some advantages, but many developers who choose web site projects eventually find that the disadvantages outweigh any perceived advantages. In addition, as new ASP.NET features are developed, they won’t always be made available for web site projects. For example, the next Visual Studio release after Visual Studio 2012 will have new tooling for creating web projects, and this new tooling will work only with web application projects.
I'd note that that page doesn't mention that, yes, Web Site projects add references just by copying into the bin folder.

How to share references to dlls across multiple projects in a visual studio solution?

I have multiple base console projects nested within a single solution in Visual Studio. Each project creates a reference to the same base dlls.
How can I create a centralized references or lib section so that all projects within the solution can reference that centralized location without having to duplicate the same base references to the same dlls over and over again for each project?
From your screenshot, most of your References appear to be .Net Framework Class Libraries. You don't have to worry about those.
If you are re-referencing the same custom libraries, it is better to include the Library's source as a Project within your Visual Studio Solution and add Project References to that Project in every other Project where it is needed.
Example: Adding a Project Reference
If you don't have access to the source code of some of the custom libraries, then what I usually do is, create a \Libraries folder at the root folder of the Solution Source folder, and add file references to the individual DLLs from the other Projects within the solution. This way, if 1 or more of your DLLs gets updated / upgraded, you just replace the copy in the \Libraries folder and all your referencing projects will point to the latest version.
You cannot avoid having to reference those libraries from each individual project. But if you are looking to do the same references for a large number of projects - or if you just have some favourite references you want added to each new project you add - then Visual Studio has that capability - it's called a template project.
Create your project the way you want it (it will most likely be a class library, although you can do a UI based one as well), then click File->Export Template, which brings up the following wizard:
Then the next time you go File -> Add -> New Project your new template will be listed just like my example on here:
If you already have a whole lot of established projects that you want to add the same reference to (and it isn't available as a nuget package so you can't use the nuget package manager), then there is a more hardcore way. Visual Studio project files are XML, so you can right click on a project in your Solution Explorer, select Unload Project, then right click again on the project and select Edit to open the project file as XML. You can then copy paste the Reference nodes containing the references you want to propagate. Do be careful to adjust any relative paths where necessary.
If you need to do this in a bulk way then open the project files in Notepad++ and do your copy paste operations.
You can't escape it. You need references in your project, you will have to reference them. If you have third party dlls then you can put them in a single folder and reference them in desired projects from there.

Unable to add project reference in Visual Studio 2015

I previously asked a similar question regarding Visual Studio 2013 but now I'm running into the same problem with Visual Studio 2015 but it seems to work differently, which makes the previous answer not applicable to this question. This is my previous question: Visual Studio 2013 - How to debug a project within a solution
I'm attempting to add a project reference. I have a library I'm working on in a separate solution/project and a console application I want to use to test and debug the library.
I tried: Add Reference > Projects > Solution > Browse
But the open file dialog does not contain .sln files in the filter list and if I force it to display them by typing *.sln and then select the Solution I get:
"Please make sure the file is accessible and that it is a valid assembly or COM component."
What am I doing wrong? How do I properly add a Solution/Project reference?
For me issue was due to difference in Target framework. One was .NET Framework 4.5 and other was .NET Framework 4.5.2
Verify if the Target framework is same for your project and the project reference that you are trying add reference.
(Right click on project -> properties -> Application Tab(Target frame work))
Right click on your solution file in Solution Explorer window, click Add and select Existing Project...
Once the project is a part of the solution, you will be able to add project reference.
In case anyone else runs into this here is something that I found. I had almost the exact same issue in VS 2015. I created a console app and then a class library project but could not add a reference to the class library.
It turns out I didn't create a class library project as I had thought, but rather a class library (package) project. These are different things and I was unaware of what the (package) part meant when I selected it. This is for creating Nuget packages, apparently.
I'm not sure if this is something new to VS 2015 but I had not run across it before and it caused me some grief. The error message ('A reference to ClassLibrary1 could not be added') was totally worthless in trying to debug it.
You should add project to your solution first; after adding project to your solution, you can reference it.
To add an existing project to your solution:
Add reference menu:
Add references dialog:
In order to add a project reference, the project has to be part of the solution.
For me issue was due to a difference in the types of projects. One was a C# Windows forms project and the other project was a C# Universal Windows project.
The Windows Forms project couldn't reference the Universal Windows project. The error was "Unable to add a reference to project "XYZ".
It appears you can have a library shared between both types of projects by folloing this answer: What kind of class library works with both Universal and Winform projects?
I ended up having a different solution. I had three projects in the solution which referenced a NuGet package which was no longer available. I had the library locally but I was unable to add it. I found that because of the BUILD ORDER and DEPENDENCY of my projects, I had to add it to the project that was built first, then I could add it to the projects that were dependent on that project.
I had this problem today and I looked at the configuration manager for the solution. The projects had different platform settings. I made them both Any CPU and rebuilt. Then I could reference the project.

C# access class library from windows form application Two projects within one solution

Is there a way for me to reference to a class library project, which is in the same solution as my windows form application ?
There are 5 windows form applications in my solution with one class library. Is it possible for me to reference to the class library without building it separately and copying the DLL each time.
Screen shots are for VS 2013
The steps to follow work for pretty much all versions
Right click the References section in the solution explorer of the project you want to add the dll to
Select Add references, select "Solution" on the left hand side
You should see your project, check it to add a reference that will always be the latest compiled version of that project.
See MSDN for more on the Add References dialog

How to include source instead of a library in C# / Visual Studio?

I come from a long Java background and very new to C#. I was trying to run one of the starter kit applications from Windows phone and I am getting this error.
A first chance exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.WindowsPhone.dll
I am finding it hard to debug this because I am directly referring to the dll file under References pointing to Newtonsoft.Json.WindowsPhone. In Java, I would have searched for the library and downloaded the source to step into the exact location during debugging.
If that is possible, how would I do it in C# (as in how would i refer the library source for my project to pick up at run-time)? I am using the Visual studio 2010 Express Edition as the IDE.
Download the third party library you are dealing with
Add this library's project into your solution
In your project, remove the reference to the 3rd party library
Add a project dependency into your project, make it depend on the 3rd party library project you have just added to your solution
Compile and run, happy debugging
My best guess is you should download last release of Json.NET, remove the compiled library from your project's references and add reference to the source code project. (Add Reference... > Projects > Browse...)
Once you have stopped your program in the debugger, you can use the modules window to load the symbols for Json.NET. Obviously, you need to have the symbols on your machine so you can browse to them.
Failing that, you can switch to a project reference and include Json.NET in your solution as Dan suggested.
The easiest way is to download their latest build then inside visual studio right click your solution and under add menu select existing project point to project file of the library and click open button. after that u will be able to set brakepoint wherever u want.

Categories

Resources