I'm been investigating options available to me for referencing existing .NET Framework 4.6 libraries in new .NET Core App projects.
As a proof of concept I've built a very simple .NET Framework 4.6 library, published it as a NuGet package and included it in a simple .NET Core Web app.
As of now the only way I seem to be able to get the project building and running is to remove the Microsoft.NETCore.App reference from the project.json dependencies block and replace the netcoreapp1.0 section from the frameworks property with a net461 block.
Full project.json below:
{
"dependencies": {
/*"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},*/
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"FileIOLibrary_1_0_0_0": "1.0.0"
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
/*"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8",
"net461"
]
}
},*/
"frameworks": {
"net461": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
This basically means that all references are .NET Framework 4.6 libraries.
Anyway I've a few questions regarding other options available to me:
I'm afraid you cannot load a .NET 4.6.2 DLL into a .NET Core application.
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.
Assuming you have a MyProject.csproj
in a folder ./NET462/MyProject/
and a solution MySolution.sln
.
MyProject.Core
and we'll put it in ./Core/MyProject.Core/
. We'll also have a Solution too, call that ./Core/MySolution.Core.sln
../NET462/MyProject/
into ./Core/MyProject
, including the .csproj. Copy the solution too, they're going to live side-by-side.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
}
}
.csproj
and MyProject.project.json
.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.
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With