Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue adding reference to class library project in ASP.NET 5 (Core)

Hi firstly i know vaguely similar questions have been asked before, but they are outdated now, I am using Visual Studio 2015 rtm and ASP.NET 5 beta 6.

I'm trying to add a reference to a normal (i.e. not vnext) class library project to my vnext web application. If I follow these steps:

  1. Create a new web app project

  2. Remove the "dnxcore50" framework from project.json

  3. Add a new project for a normal class library

  4. Manually move the class library project into the /src folder (otherwise I get error "The dependency MyClassLibrary1 >= 1.0.0-* could not be resolved.")

  5. Add a reference to this class library

Now it builds OK, but if I try and add "using MyClassLibrary1" it says MyClassLibrary1 doesn't exist in current context.

If I then change the class library to target .NET 4 Client profile (by default it was 4.6) it does work correctly, however .NET 4 full or 4.5 does not work. I need it to be 4.5 or higher as I need to reference various packages that require this. Ideally everything would just target 4.6.

This is my project.json file:

{
  "webroot": "wwwroot",
  "userSecretsId": "aspnet5-WebApplication2-6767111e-0eba-42a4-9d68-4b6c20767518",
  "version": "1.0.0-*",

  "dependencies": {
    "EntityFramework.SqlServer": "7.0.0-beta6",
    "EntityFramework.Commands": "7.0.0-beta6",
    "Microsoft.AspNet.Mvc": "6.0.0-beta6",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta6",
    "Microsoft.AspNet.Authentication.Cookies": "1.0.0-beta6",
    "Microsoft.AspNet.Authentication.Facebook": "1.0.0-beta6",
    "Microsoft.AspNet.Authentication.Google": "1.0.0-beta6",
    "Microsoft.AspNet.Authentication.MicrosoftAccount": "1.0.0-beta6",
    "Microsoft.AspNet.Authentication.Twitter": "1.0.0-beta6",
    "Microsoft.AspNet.Diagnostics": "1.0.0-beta6",
    "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-beta6",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-beta6",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta6",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta6",
    "Microsoft.AspNet.StaticFiles": "1.0.0-beta6",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-beta6",
    "Microsoft.Framework.Configuration.Abstractions": "1.0.0-beta6",
    "Microsoft.Framework.Configuration.Json": "1.0.0-beta6",
    "Microsoft.Framework.Configuration.UserSecrets": "1.0.0-beta6",
    "Microsoft.Framework.Logging": "1.0.0-beta6",
    "Microsoft.Framework.Logging.Console": "1.0.0-beta6",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-beta6"
  },

  "commands": {
    "web": "Microsoft.AspNet.Hosting --config hosting.ini",
    "ef": "EntityFramework.Commands"
  },

  "frameworks": {
    "dnx451": {
      "dependencies": {
        "MyClassLibrary1": "1.0.0-*"
      }

    }
  },

  "exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
  ],
  "publishExclude": [
    "node_modules",
    "bower_components",
    "**.xproj",
    "**.user",
    "**.vspscc"
  ],
  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ]
  }
}

And my global.json file:

{
  "projects": [
    "src",
    "test",
    "wrap"
  ],
  "sdk": {
    "version": "1.0.0-beta6"
  }
}
like image 819
Martin Richards Avatar asked Aug 04 '15 08:08

Martin Richards


3 Answers

This is how I did it using beta6 (UPDATE: It's still valid for the RC1 UPDATE 1).

  1. Remove frameworks => dnxcore from your project.json (you can't target it anyway using full .net class libraries)
  2. In your target project right click on References => Add Reference
  3. Navigate and select reference dll you want to add.

This will add a reference entry to your project.json file. Behind the scenes the dll is copied over to /lib directory in your solution and a "wrapper project" with only a project.json file is created in /wrap folder. The wrapper project is documented here (not well enough though): https://github.com/aspnet/Home/wiki/Project.json-file#bin-syntax-wrapping-a-dll

That's it! I've just tested this scenario. Hope this helps.

like image 111
mbudnik Avatar answered Oct 07 '22 05:10

mbudnik


If anyone else is struggling with this particular error, the key is to add the reference in by "browsing" to the actual file when adding the reference, and not using the "project" tab.

This doesn't seem to store the path, but adds the reference to the project.json as per normal. Obviously a bug (beta7) at the time of writing this.

like image 20
JamesDill Avatar answered Oct 07 '22 05:10

JamesDill


Here is how I did it in ASPT.NET 5 RC 1 Update 1:

  1. Add the project reference
  2. Open project.json, inside "frameworks": node, delete "dnxcore50": { } line (JSON does not allow comment). When you save the json file, the DNX Core 5 reference should be removed immediately.

  3. Build the project. It should build successfully.

  4. Put "dnxcore50": { } back. It does not bring DNX 5 Core back.

like image 23
Jeson Martajaya Avatar answered Oct 07 '22 03:10

Jeson Martajaya