Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2015 add reference for Class Library

I have created a project in VS2015, structure as below:

Solution1

  • BookStore.ClassLibrary1 => Class Library (Package)
  • BookStore.ClassLibrary2 => Class Library
  • BookStore.Web => MVC5

In BookStore.Web, I can reference BookStore.ClassLibrary2, but fail to reference BookStore.ClassLibrary1.

It shows an error "A reference to 'ClassLibrary1' could not be added."

My question is how to reference a Class Library (Package) in VS2015? Thank you so much!

like image 383
Ricky Yip Avatar asked Aug 04 '15 09:08

Ricky Yip


People also ask

How do I add a reference to a Class Library?

Right-click the newly added project (not the Class Library project) and select Add Reference. The Add Reference dialog box appears. Click the Projects tab of the Add Reference dialog box and select your Class Library project. Click OK.

How do I add a reference to .NET core library?

One method of adding references to your library is by typing it directly in the project. json file. As you can see that we have added some references under the dependencies section as shown in the following code. Let us now save this file and you will see that references are added to your library now.

How do I add a reference to a class in Visual Studio?

Restart Visual Studio and open your app. Right-click on the References or Dependencies node in the project that caused the error and choose Add Reference.


2 Answers

Looks like your ClassLibrary1 project is a Class Library Package, not a class library project. Class Library Package is used to create Nuget packages that can target any platform.

There are a number of benefits of ASP.NET 5 Class Library projects (.kproj) over Class Library projects (.csproj):

ASP.NET 5 class libraries easily support cross-compiling projects to multiple targets, such as aspnet50, aspnetcore50, net45, and various other portable class library variations. This includes rich Visual Studio support for Intellisense to notify you which APIs are available for which targets. NuGet packages are automatically created, which is an extremely common thing to do with class libraries. Better productivity when it comes to things like automatically refreshing Solution Explorer when the file system changes. Fewer conflicts in source control when trying to merge conflicting changes in the *.csproj file. Can be compiled cross-platform (in part because it doesn't depend on MSBuild) You can reference a *.csproj project from a *.kproj project (this was just made a lot easier with the new preview of Visual Studio 2015), but it was always possible with some manual steps.

Why does the name have "ASP.NET" in it?

As far as the names goes, it's a relic of history that will soon be addressed. The new project type is useful far beyond ASP.NET 5 applications. Expect to see new names in a future preview of Visual Studio:

.NET Console Application (Cross-platform) .NET Class Library (Cross-platform) With the release of Visual Studio 2015 RC you can see the updated project template names:

Class Library (Package) Console Application (Package) These use the project.json file and the .NET Execution Environment (DNX) to build, run, and package (into a NuGet package) the project.

These project templates continue to show up in the New Project dialog under the "Web" node, but now also show up in the main "Visual C#" node as well.

Here is a good link as you need to referance a dll that the new clas library does not build. https://evolpin.wordpress.com/2015/01/25/vnext-and-class-libraries/

like image 77
pool pro Avatar answered Sep 16 '22 19:09

pool pro


Either use a plain old class library or use a Nuget class library, publish it to a local or public Nuget repo and add it to the web project from there.

like image 26
citykid Avatar answered Sep 20 '22 19:09

citykid