Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core RC2 Project Reference "The Dependency X could not be resolved"

Overview

I've got an ASP.NET Core RC2 .NET framework web project, and I'd like to add a project reference to my regular C# class library contained within the same solution.

Steps to repro:

Using Visual Studio 2015 Update 2

File -> New Project -> ASP.NET Core Web Application (.NET Framework)

Right click solution -> New Project -> Class Library

I'm not making any of these:

  • Class Library (.NET Core)
  • Class Library (Portable for iOS, Android, and Windows)
  • Class Library (Portable)

Add the following to dependencies in project.json:

"ClassLibrary1": {
  "version": "*",
  "target": "project"
}

Issue

Why can I not add "target":"project" to my dependencies when specifying a project dependency?

enter image description here

Expectation

I expect this ASP.NET Core RC2 web application (.NET Framework) to be able to reference a regular class library as a project reference.

This works

"ClassLibrary1": "*"

This does not work

"ClassLibrary1": {
  "version": "*",
  "target": "project"
}

My Question

How do I add a project reference to my regular class library from an ASP.NET Core RC2 web project?

Additional Information

If I run dotnet restore I get a better error message on why this can not be resolved.

dotnet : 
At line:1 char:1
+ dotnet restore
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Errors in C:\users\joshs\documents\visual studio 2015\Projects\WebApplication4\src\WebApplication4\project.json

    Unable to resolve 'ClassLibrary1' for '.NETFramework,Version=v4.6.1'.

I doubled checked the class library targets .NET Framework 4.6.1

enter image description here

I've already taken a look at cannot add reference to .net core Class library asp.net core rc2, but that's for a .NET Core class library.

I also looked at Reference a Full Framework Library Project from ASP.NET Core MVC Web Application (RC2)?, but that's because the user was trying to create a web project not targeting .NET Framework. My project.json contains:

  "frameworks": {
    "net461": { }
  },

If I right click my web project and 'Add reference' and then proceed to pick my class library, it puts the project dependency in a different part of the project.json, but it still gives me the same error message.

"frameworks": {
  "net461": {
    "dependencies": {
      "ClassLibrary1": {
        "target": "project"
      }
    }
  }
},
like image 866
Josh Schultz Avatar asked May 25 '16 22:05

Josh Schultz


1 Answers

You can reference:

  • A classic C# class library from a project.json targeting full dotnet framework
  • A PCL C# class library from a project.json targeting full dotnet framework and/or CoreCLR

If your C# class library is not PCL then you can only reference it from the full dotnet section (net*) in project.json

To reference from VS, right click on the project -> References -> Select the project

like image 61
Victor Hurdugaci Avatar answered Oct 10 '22 09:10

Victor Hurdugaci