chdev77
chdev77

Reputation: 555

Referencing .NET 4.6.2 class library from .NET Core app

I'm using VS Update 3 (14.0.25425.01). Here's what I've done:

  1. Create ASP.Net Core Web Application (.Net Core)
  2. Create Class Library .Net 4.6.2
  3. Add net462 to frameworks, netcoreapp1.0, imports in project.json
  4. Right click on ASP.Net Core app, click Add Reference, select Projects, select Class Library you created in step 2.

I get no errors on restoring and the reference is added to the ASP.Net Core app. However, I cannot access it. I cannot add a using import declaration or access the objects. I've ran through many things but nothing seems to work and the posts are very versioned fragmented.

enter image description here

Here is the Program.cs in the ASP.Net Core App. enter image description here

enter image description here

Update I did what Nate suggested. I thought I tried this already..but sure enough I can now access my 4.6.2 libraries. However, I'm now getting compile errors.

enter image description here

Upvotes: 8

Views: 7408

Answers (2)

chdev77
chdev77

Reputation: 555

@NateBarbettini answer accomplished my original question. But I could not run ASP.Net Core Web Application version 1 with my .Net 4.6.1 project as it was because it could not find a .NetCore.App v1 assembly for my .Net 4.6.1 project. So I added a project.json to my .Net 4.6.1 project with the following project.json.

{
  "version": "1.0.0-*",

  "dependencies": {
  "NETStandard.Library": "1.6.0"
  },

  "frameworks": {
  "netstandard1.6": {
    "imports": "dnxcore50"
   },
  "net461": {}
 }
}

Next, in the ASP.Net Core Web Application modify the project.json by adding a dependency under the .NetCore.App. This way it will pick up both versions, 4.6.1 and .NetCore v1.

...
"frameworks": {
"net461": {
  "dependencies": {
    "ClassLibrary1": {
      "target": "project"
    }
  }
},
"netcoreapp1.0": {
  "imports": [
    "dotnet5.6",
    "portable-net45+win8",
    "net461"
  ],
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "ClassLibrary1": {
      "target": "project"
    }
  }
}
}

So far so good, I can develop in .Net 4.6.1 and it will work running under .NetCore.App v1. However, I think there will be issues when I have other dependencies in my .Net 4.6.1 projects.

Upvotes: 1

Nate Barbettini
Nate Barbettini

Reputation: 53600

This does work in Visual Studio 2015 Update 3, but your project.json isn't quite right.

Instead of adding net462 to the imports section, it should be in the frameworks section:

"frameworks": {
  "net461": { },
  "netcoreapp1.0": {
    "dependencies": {
      "Microsoft.NETCore.App": {
        "type": "platform",
        "version": "1.0.0"
      }
    }
  }
}

Notice that the Microsoft.NETCore.App dependency also needs to be moved into the netcoreapp1.0 section. That's because this dependency is only required when compiling as a .NET Core application.

The reference to your .NET 4.6.2 library is then simply part of your dependencies section:

"dependencies": {
  (...)
  "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
  "MyLibrary": {
    "target": "project"
  }
}

By structuring it this way, I was able to reference and use classes in my .NET 4.6.2 library without any problems.


For reference, here's the entire working project.json I used:

{
  "dependencies": {
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "MyLibrary": {
      "target": "project"
    }
  },
  "frameworks": {
    "net461": { },
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      }
    }
  },
  "version": "1.0.0-*"
}

Upvotes: 4

Related Questions