Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 346
chdev77 Avatar asked Aug 08 '16 18:08

chdev77


People also ask

How do I add a reference to class library in .NET Core?

Add Class Library Reference To use a class library in your application, you must add a reference to the library to access its functionality. Right click on the project name of your console app in Solution Explorer and select Add ->Reference option.

Can I use .NET framework library in .NET Core project?

The answer is no, we cannot use . NET Framework Base Class Library in . NET Core because of compatibility issues. Basically, the libraries which target .

Can I reference .NET framework in .NET Core?

If the dependencies are not NuGet packages, the ApiPort tool can check the portability of the dependency. Since . NET Core 2.0, a compatibility shim allows referencing . NET Framework packages that haven't been switched to use .

How do I use .NET standard library in .NET Core?

Open Visual Studio 2015 Go to menu click File> New> Project then choose . net Core from left menu by selecting ASP.NET Core Web Application like below image. I have chosen an ASP.NET Core sample Template, like the below image. Let's create another project as a simple class library.


2 Answers

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-*"
}
like image 72
Nate Barbettini Avatar answered Oct 01 '22 15:10

Nate Barbettini


@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.

like image 27
chdev77 Avatar answered Oct 01 '22 15:10

chdev77