Reference a .NET Core Library in a .NET 4.6 project
The answer is shown in a presentation
Only with ASP.NET Core RC 2 which is not released yet, a support for referencing xproj in csproj will be added.
Referencing Framework 4.6 Library in in .NET Core App 1.0.0 project
I'm afraid you cannot load a .NET 4.6.2 DLL into a .NET Core application.
The alternative plan
In some circumstances, you can write the code once and then have a project for .NET 4.6.2 and a project for .NET Core. Both projects point at the same code files but each project has a different framework target. Once you have that working, you can build a nuget package that has both the .NET 4.6.2 DLLs and the .NET Core DLLs in it, which will make deployment and build much simpler.
How to get the same code files building for each framework
Assuming you have a MyProject.csproj
in a folder ./NET462/MyProject/
and a solution MySolution.sln
.
- First create an empty .NET Core Library. We'll call it
MyProject.Core
and we'll put it in./Core/MyProject.Core/
. We'll also have a Solution too, call that./Core/MySolution.Core.sln
. - Now copy all the files from the original
./NET462/MyProject/
into./Core/MyProject
, including the .csproj. Copy the solution too, they're going to live side-by-side. - If you load the original solution right now then Visual Studio will throw errors because there will be a conflict between the project.json (which is used by the Core project) and the .csproj (which is used by the .NET 4.6.2 project).
- To stop that, create a new file called
MyProject.project.json
. This acts as a companion to the project.json when the .NET 462 project is loaded. Put the following into it:
MyProject.project.json
{
"version": "1.0.0-*",
"description": "My Class Library",
"authors": [
"Your Name"
],
"tags": [
""
],
"projectUrl": "",
"licenseUrl": "",
"runtimes": {
"win": {}
},
"frameworks": {
"net452": {} // You might want net462
},
"dependencies": {
"Newtonsoft.Json": "9.0.1" /// Just an example of a nuget package
}
}
- Your .NET 4.6.2 project should now build just as before.
- Now open the Core solution (it's OK to have both Visual Studios open at the same time) and you'll see that all the files are added in there. Under .NET Core / xproj, Project.json files are include-by-default. You will need to ignore the files specific to .NET 462, which are the
.csproj
andMyProject.project.json
. - It probably won't build until you get the nuget packages added.
How to get .NET Core and .NET into the same Nuget
- Once each solution is built in Release mode, you will have two outputs, a DLL for each of the frameworks. You can change the name of the DLLs in the project files (.csproj and project.json respectively) but nuget doesn't care.
Create a
package.nuspec
file and put the following in it:<!-- language: lang-xml -->
<?xml version="1.0"?>
<package>
<metadata>
<id>MyProject</id>
<version>1.0.0</version>
<authors>Me</authors>
<owners>Me</owners>
<projectUrl></projectUrl>
<iconUrl>FILL ME IN</iconUrl>
<description>FILL ME IN</description>
<copyright>FILL ME IN</copyright>
<releaseNotes>First attempt</releaseNotes>
<tags>FILL ME IN</tags>
<dependencies>
<group targetFramework="dotnet">
<dependency id="Newtonsoft.Json" version="9.0.1" />
</group>
<group targetFramework="net452">
<dependency id="Newtonsoft.Json" version="9.0.1" />
</group>
</dependencies>
<frameworkAssemblies>
<frameworkAssembly assemblyName="System.Core" targetFramework="net452"/>
<frameworkAssembly assemblyName="System.Core" targetFramework="dotnet" />
</frameworkAssemblies>
</metadata>
<files>
<file src="Core\MyProject\bin\release\MyProject.dll" target="lib/net452" />
<file src="Core\MyProject\bin\release\MyProject.Core.dll" target="lib/dotnet" />
</files>
</package>Use this to build your nuget package and when including in your main system, nuget will serve the correct package.
Bigger Example
My JsonApi library does this and additionally it has two Web projects, one for .NET 4.6.2 and one for .NET Core because they use different base classes. The main shared library is used by both.
Big caveat
Some libraries that your .NET 4.6.2 rely upon will not be available to your Core application. The two times I've done this it has been System.Web
that has caught me out. System.Web
in .NET 4.6.2 is really a wrapper for IIS. The best way to find out is to try merging the projects above and keep adding nuget packages to project.json
until you hit something that doesn't exist for Core.
Things are changing
The .NET team in May 2016 said that they were moving away from using the project.json
at the end of 2016. At which point, I imagine that these problems might go away as visual studio should be able to handle two kinds of .csproj
living side by side. Depending on your commercial situation, I'd wait for that change!
Referencing .NET 4.6.2 class library from .NET Core app
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-*"
}
Mixing .Net Framework app with .Net Core library
I need to have a [...] .Net framework 4.0 depending on a .Net Core library project.
Is there a way to have both projects build and run together?
No
Sorry. That's not the way it works. You cannot directly reference a .NET Framework assembly from .NET Core or the other way round a .NET Core assembly from .NET Framework.
(I have a slight deja vu, I will try to find the duplicate on this).
What you can do is create a shared assembly in .NET Standard that both can reference, but in this case it would not actually help you, because you would need a higher .NET Framework version to be able to use .NET Standard 2.0.
What would work is creating a connection between your assemblies that is not a direct assembly binding via reference. One could call the other via REST over HTTP or via memory mapped files or in any other way that you would use to have two otherwise incompatible technologies communicate.
Reference a .Net 4.6.2 project in a .NetCore project (VS2017 15.4.4)
I got two problems:
- As said @Jamie Taylor, I needed to downgrade to .NET Framework 4.6.1 to be compatible with .NET core 2.0.
- Because of downgrading, some Nuget packages where conflicting with the .NET Framework version 4.6.1. It was the case of EF 6.2 (which uses .NET Framework 4.6.2). So I downgraded all needed packages then deleted "packages", "bin" and "obj" folders and did a Nuget Restore.
Related Topics
C# - Which Is the Best Alternative to 'Switch on Type'
Serializable Classes and Dynamic Proxies in Ef - How
What Task Is Best Done in a Functional Programming Style
Newtonsoft Inline Formatting for Subelement While Serializing
Intersect with a Custom Iequalitycomparer Using Linq
How to Open a Web Page from My Application
How Better to Resolve Dependencies in Object Created by Factory
Determine Operating System in .Net Core
Linq Syntax - Selecting Multiple Columns
Can Connect to Ftp Using Filezilla or Winscp, But Not with Ftpwebrequest or Fluentftp
Prevent C# App from Process Kill
Converting Integers to Roman Numerals
How to Capitalize First Letter of First Name and Last Name in C#
Including Pictures in an Outlook Email
Custom Xmlwriter to Skip a Certain Element
Should the Repository Layer Return Data-Transfer-Objects (Dto)
Httpclient Single Instance with Different Authentication Headers