12

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!

Community
  • 1
  • 1
Ricky Yip
  • 303
  • 1
  • 4
  • 11
  • What is the error description when you added a reference? – Karthik Aug 04 '15 at 09:56
  • i guess its not spelling mistake, I tick the checkbox in popup window, also I have added the error screenshot. thanks! – Ricky Yip Aug 04 '15 at 09:59
  • Sometimes I encounter similar errors, sometimes they origin from VS Extension that want to take some operation upon adding the reference and fail, causing everything to fail. You can try this: edit the project file of your web project manually and see if the reference works then. You can edit the csproj externally or inside VS: unload the project, edit, reload the project (all in context menu of solution explorer) – citykid Aug 04 '15 at 10:11
  • Thanks citykid, I have tried to add ClassLibrary1 manually, after reload the project, "!" appear beside the ClassLibrary1... – Ricky Yip Aug 04 '15 at 10:23
  • Ok, that helps, then there is apparently really something with the libs. Find each of the 3 actually used dlls and open them with ildasm or dotpeek and check which clr they depend on. you can also check your build settings in VS to make sure all have the same target .Net runtime and are all set to AnyCPU. – citykid Aug 04 '15 at 10:28
  • Also, if you load the solution now, maybe the output window says anything about the "!". – citykid Aug 04 '15 at 10:29
  • it shows "Severity Code Description Project File Line Error Metadata file 'xxx\Visual Studio 2015\Projects\AcIntranet2\artifacts\bin\ClassLibrary1\ClassLibrary1.exe' could not be found MvcApplication xxx\Visual Studio 2015\Projects\AcIntranet2\MvcApplication\CSC" – Ricky Yip Aug 04 '15 at 10:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85097/discussion-between-citykid-and-ricky-yip). – citykid Aug 04 '15 at 10:39

3 Answers3

7

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/

pool pro
  • 2,084
  • 12
  • 21
  • yes, but can I reference "Class Library Package" project (*.kproj) in my web application? just like class library project (*.csproj). – Ricky Yip Aug 04 '15 at 10:53
  • as citykid mentioned, you can use it a a portable class library after it is published. it's currently not possible to reference project.json-projects from csproj-projects – pool pro Aug 04 '15 at 11:08
  • 1
    This doesn't answer the question at all. The OP is trying to reference a "Class Library (Package)" from a regular .NET app; this answer doesn't explain how to do that, and the linked article only explains how to do the opposite (reference a regular .NET lib from an ASP.NET 5 app). – Thomas Levesque Jan 14 '16 at 19:34
1

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.

citykid
  • 9,916
  • 10
  • 55
  • 91
0

I doubt about dependency of ClassLibrary1 and I can even see that in your screenshot,enter image description here

It seems ClassLibrary1 is looking for some dependent dlls, so you might need to add those dll first then you can go ahead and add it. Something similar happens here too

More details about Depencies can gather from this MSDN link you can directly jump to Dependencies node for Bower and NPM dependencies

Community
  • 1
  • 1