I am afraid that you cannot load DLL.NET 4.6.2 into a .NET Core application.
Alternative plan
In some cases, you can write the code once, and then create a project for .NET 4.6.2 and a project for .NET Core. Both projects point to the same code files, but each project has a different target structure. After that, you can create the nuget package, which has both .NET 4.6.2 libraries and .NET Core DLLs, which will simplify deployment and assembly.
How to get the same code file files for each structure
Assuming you have MyProject.csproj in the ./NET462/MyProject/ folder and in the MySolution.sln solution.
- First create an empty .NET Core library. We will call it
MyProject.Core , and we will put it in ./Core/MyProject.Core/ . We will also have a solution, call it ./Core/MySolution.Core.sln . - Now copy all the files from the source
./NET462/MyProject/ to ./Core/MyProject , including .csproj. Copy the solution too, they will live side by side. - If you download the original solution right now, then Visual Studio will throw errors because there will be a conflict between project.json (which is used by the base project) and .csproj (which is used by .NET 4.6.2).
- To stop this, create a new file called
MyProject.project.json . This acts as a companion for project.json when the .NET 462 project loads. Paste 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 be built as before.
- Now open the Core solution (it's normal for both Visual Studios to be open at the same time) and you will see that all files are added there. In .NET Core / xproj, Project.json files are included by default. You will need to ignore .NET 462 specific files, which are
.csproj and MyProject.project.json . - It will probably not be created until you add nuget packages.
How to get .NET Core and .NET in the same Nuget
- As soon as each solution is created in release mode, you will have two outputs - a DLL for each of the frameworks. You can change the name of the DLL in the project files (.csproj and project.json respectively), but nuget does not care.
Create the package.nuspec file and put the following into it:
<?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 create your own nuget package, and when included in your main system, nuget will serve the correct package.
Higher example
My JsonApi library does this and additionally 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 reservation
Some libraries that your .NET 4.6.2 relies on will not be available to your main application. Two times when I did this, it was System.Web that gripped me. System.Web in .NET 4.6.2 is indeed a wrapper for IIS. The best way to find out is to try to merge the projects above and keep adding nuget packages to project.json until you click on something that doesn't exist for Core.
Things are changing
The .NET team in May 2016 reported that they had project.json use of project.json at the end of 2016. At this point, I believe that these problems may disappear, as the visual studio should be able to handle two kinds of .csproj living side by side. Depending on your business situation, I would wait for this change!
source share