Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reference a .NET Framework project in a .NET Core project?

I'd really like to start using .NET Core and slowly migrate applications and libraries to it. However, I can't realistically upgrade my entire code base to use .NET Core and then go through the process of testing and deploying a plethora of applications in production.

As an example, if I create a new .NET Core application and try to reference one of my .NET Framework projects I get the following:

The following projects are not supported as references: - Foobar.NetFramework has target frameworks that are incompatible with targets in current project Foobar.NetCore.

Foobar.NetCore: .NETCoreApp,Version=v1.0

Foobar.NetFramework: .NETFramework,Version=v4.5

Is it possible to create a new .NET Core application and reference my existing .NET Framework libraries? If so, what's the process for doing that? I've spent hours going through Microsoft's documentation and searching their issues on GitHub, but I can't find anything official on how to achieve this or what their long-term vision is for this process.

like image 435
Justin Helgerson Avatar asked Jul 01 '16 14:07

Justin Helgerson


People also ask

Can I reference .NET Framework in .NET core?

NET Core, you need to use the . NET Framework. You can now reference . NET Framework libraries from .

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 .

How do I add a project reference in net core project?

One method of adding references to your library is by typing it directly in the project. json file. As you can see that we have added some references under the dependencies section as shown in the following code. Let us now save this file and you will see that references are added to your library now.


2 Answers

Old question, but with the release of .NetStandard 2.0 and .netcore 2.0 and vs2017.3, the game has changed.

You can use the Full .NET Framework (TFM) with .NetCore 2.0, but how?

  1. In Visual Studio 2017.3, you can reference the Full .NET Framework (any version) directly from within a .NetCore2 project.

  2. You can build the .NetStandard2 class library and reference your TFM. Then reference your .NetStandard2 library from your .NetCore2 project.

For example, referencing json.net net45 from .NetStandard2. Browse to the folder and select version net45 (not netstandard1.3)

See the dependency in the image below, no yellow warning as you see.

enter image description here

  1. Even if a Nuget library is not ready to be ported to .Netstandard 2, you can use any API in the library that is compliant to net461.

Quoting for the .NET Core 2/Standard 2.0 announcement with links:

.NET Core 2.0 is able to freely reference libraries that have been built for .NET Framework up to version 4.6.1

However, some libraries may fail at run time if they try to use API methods that aren't available on .NET Core

Reference: .NET Core App target .NET framework 4.5.2 on Linux

A need to use third-party .NET libraries or NuGet packages not available for .NET Core

So only in cases where the libraries or NuGet packages use technologies that aren't available in .NET Standard/.NET Core, you need to use the .NET Framework.

Reference: Choosing between .NET Core and .NET Framework for server apps

You can now reference .NET Framework libraries from .NET Standard libraries using Visual Studio 2017 15.3. This feature helps you migrate .NET Framework code to .NET Standard or .NET Core over time (start with binaries and then move to source). It is also useful in the case that the source code is no longer accessible or is lost for a .NET Framework library, enabling it to be still be used in new scenarios.

Reference: Announcing .NET Core 2.0

like image 143
M.Hassan Avatar answered Sep 20 '22 20:09

M.Hassan


Yes, we are currently attempting the same thing. The trick is to make sure that you are supporting the same .NET frameworks. Inside your project.json file, make sure the framework matches the framework of the project you wish to include. For example:

"frameworks": {     "net46": {  --This line here <<<<       "dependencies": {         "DomainModel": {           "target": "project"         },         "Models": {           "target": "project"         }       }     }   }, 

FYI: You might need to change the framework of your .NET Core or your older projects to achieve this. .NET Core can be changed just by editing the project.json file as seen above. You can so the same in .NET projects by right clicking the project and opening properties. Change the framework level there.

Once you have matched the two project frameworks then you should be able to include them. Good Luck!

like image 31
aholtry Avatar answered Sep 23 '22 20:09

aholtry