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

4

79

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.

Ataghan answered 1/7, 2016 at 14:38 Comment(3)
This question may be of some use to you: #39041597Epicenter
@CamiloTerevinto any update on whether it can be done with .Net 5?Interstellar
You have to port your libraries to .Net Standard. You can use dotnet.microsoft.com/en-us/platform/upgrade-assistant to identify more indepth details.Ashtonashtonunderlyne
P
18

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!

Pimply answered 1/7, 2016 at 14:55 Comment(9)
And, to be specific because I think this could be confusing, this is running ASP.NET Core on the full .NET Framework as opposed to running ASP.NET Core on what's known as .NET Core. The distinction between .Net Framework and .Net Core is important because the first is running the latest ASP.NET on what we have in the past, and the other is running on a smaller .Net component that is cross-platform.Gabriellegabrielli
I'm still new to the project.json schema and the documentation seems sparse. If I have a project called "My.Test.Project" how would I add that given your example above?Ataghan
When I try to add the dependency into the project.json file I get the following: Unable to resolve 'My.Test.Project (>= 1.0.0)' for '.NETFramework,Version=v4.6'. The project is not a NuGet package if that makes a difference.Ataghan
@BradRem Any chance you have an idea?Ataghan
@JustinHelgerson, If you're using Visual Studio, trying adding it like you used to: by clicking on the References item and saying Add Reference. The IDE will create the entry in project.json for you. It should place it under the net46 if your Framework project is .net46.Gabriellegabrielli
@BradRem Thanks for the reply. When I try to do that I get the error posted in my question. I changed the project that I'm trying to reference to use 4.6 instead of 4.5, but that didn't seem to make a difference (I got the same error).Ataghan
@JustinHelgerson I've made a Github repo, AspNetCoreWithFrameworkLibrary that demonstrates the very basics of adding a Framework library to Asp.Net Core web application.Gabriellegabrielli
@BradRem great github, I just tried it although this doesn't seem to work with Core WebApi projects unfortunately, can you comment? ThanksSamos
@Pimply seems this is not work when i change framework it show the errors i created new .net framework core app it also cant add the referenceBobker
J
34

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

Jurisdiction answered 24/9, 2017 at 20:17 Comment(0)
P
18

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!

Pimply answered 1/7, 2016 at 14:55 Comment(9)
And, to be specific because I think this could be confusing, this is running ASP.NET Core on the full .NET Framework as opposed to running ASP.NET Core on what's known as .NET Core. The distinction between .Net Framework and .Net Core is important because the first is running the latest ASP.NET on what we have in the past, and the other is running on a smaller .Net component that is cross-platform.Gabriellegabrielli
I'm still new to the project.json schema and the documentation seems sparse. If I have a project called "My.Test.Project" how would I add that given your example above?Ataghan
When I try to add the dependency into the project.json file I get the following: Unable to resolve 'My.Test.Project (>= 1.0.0)' for '.NETFramework,Version=v4.6'. The project is not a NuGet package if that makes a difference.Ataghan
@BradRem Any chance you have an idea?Ataghan
@JustinHelgerson, If you're using Visual Studio, trying adding it like you used to: by clicking on the References item and saying Add Reference. The IDE will create the entry in project.json for you. It should place it under the net46 if your Framework project is .net46.Gabriellegabrielli
@BradRem Thanks for the reply. When I try to do that I get the error posted in my question. I changed the project that I'm trying to reference to use 4.6 instead of 4.5, but that didn't seem to make a difference (I got the same error).Ataghan
@JustinHelgerson I've made a Github repo, AspNetCoreWithFrameworkLibrary that demonstrates the very basics of adding a Framework library to Asp.Net Core web application.Gabriellegabrielli
@BradRem great github, I just tried it although this doesn't seem to work with Core WebApi projects unfortunately, can you comment? ThanksSamos
@Pimply seems this is not work when i change framework it show the errors i created new .net framework core app it also cant add the referenceBobker
N
14

We delayed migrations as long as could as it seemed daunting as first. But we got an insistent client who wanted to migrate ASAP.

So we migrated their Fintech Web App developed on .NET Framework 4.8 Web Forms to .NET 6 Razor Page. Our team scoured though hundreds of online resources & spoke to Microsoft Tech Support before we started the project. Hope the high-level walkthrough of our journey help you plan your migrations.

Our .NET Framework Website consisted of 1 .NET Web Forms project and 12 Class Libraries.

Here is how we did it.

  • Refactored the .NET Framework 4.8 Web Forms code We ensured that the Web Forms code behind did not have a single line of service or business logic code. When we did find some business logic code in the web forms code behind, we refactored it, by moving it to the class libraries.

  • Created new .NET Standard projects We created a new .Standard 2.0 Class library project for every .NET Framework 4.8 Class Library. If the original project was called "FintechProjectName.StockMarketClient", we named the .NET standard project "FintechProjectName.StockMarketClient.Standard".

  • Copied all files from .NET framework to .NET standard We copied all the class files from .NET framework to .NET standard projects. We then removed all the .NET framework class libraries from the solution and added references to the new class libraries. All projects compiled on the 1st try itself and all our test cases too passed with minor changes.

  • Create new .NET 6 Web App Project We created a new .NET 6 Web App Project. We had to entirely redo the front-end as there is no direct path for migrating Web Forms to Razor Pages. This was the only project which took us about 1 month to migrate.

  • Reference .NET standard class libraries in the new .NET 6 website We copied all the .NET Standard libraries to this new solution containing the Razor Pages web site. Added the references and got it to work.

  • Move from .NET Standard to .NET 6 class libraries Once the new website was up and running, with all test cases passed, we did the last step in the process which was the simplest. Created .NET 6 class library projects for each of the .NET standard libraries and named the projects appropriately. Copied all class files from .NET standard projects to their corresponding .NET 6 projects. Then we removed the .NET Standard libraries and added references to the new class libraries.

Overall project timelines were about a month and a half, most of it spend on Razor Pages implementation using the same html design.

Note: If you are using any 3rd party library which does not have a .NET standard or .NET 5 version, then you are out of luck. You will need to find a replacement nuget package and recode your application to use this new library.

Nathanson answered 18/3, 2022 at 22:4 Comment(2)
In my case I was able to create a 4.8 framework winforms project and reference it from my .net6 project in the normal manner of adding a project reference.Horrible
This comment is full of useful wisdom for a command & complicated problem. I'm sad to see that it hasn't gotten more attention. Thanks for taking the time to write up this distillation of your experience.Godric
H
0

In my case with .net6 referencing framework 4.8 library ( both winforms), the trick seems to be to add the reference to the framework dll as a shared reference.

Horrible answered 15/3, 2022 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.