Link scheme 4.6. Library in the .NET Core App 1.0.0 project

I am exploring the options available to me for links to existing .NET Framework 4.6 libraries in new .NET Core App projects.

As a proof of concept, I created a very simple .NET Framework 4.6 library, published it as a NuGet package, and included it in a simple .NET Core web application.

Currently, the only way I can get the creation and launch of the project is to remove the Microsoft.NETCore.App link from the project.json dependency block and replace the netcoreapp1.0 section from the frameworks property of the net461 block.

Full .json project 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 links are .NET Framework 4.6 libraries.

In any case, I have a few questions regarding the other options available to me:

  • Is it possible to indicate that only the imported library should be targeted at the .NET 4.6 Framework, and all other links should use the .NET kernel? If so, that would mean that I could add the Microsoft.NETCore.App link back to the project.
  • If the above is not possible, can I rebuild my library as a compatible standard .NET library (I believe version 1.6 is compatible between Framework 4.6 and Core 1.0.0)? I could not see the option in Visual Studio to allow me to create libraries against the .NET Standard, so I would appreciate some pointers in this direction if it is an option.
+6
source share
1 answer

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 } } 
  1. Your .NET 4.6.2 project should now be built as before.
  2. 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 .
  3. 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:

     <!-- 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 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!

+5
source

All Articles