Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert .Net Framework 4.6.2 project to .Net core project

I Have a solution which contains the bunch of class libraries which is developed by .Net framework 4.6.2. I have to convert those class libraries into .Net core. Is there any best and fastest way to convert instead for rewrite the code.

like image 303
karthikraja Avatar asked Jan 02 '18 08:01

karthikraja


People also ask

Can you convert .NET framework to .NET Core?

You can migrate your old project to the Core project using the 'dotnet migrate; command, which migrates the project. json and any other files that are required by the web application. The dotnet migrate command will not change your code in any way.

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 .


3 Answers

This appears to be an official Microsoft resource for doing the migration. Summarized below:

  1. (recommended) Retarget all projects you wish to port to target the .NET Framework 4.7.2 or higher.

  2. (recommended) Use the .NET Portability Analyzer to analyze your assemblies and see if they're portable to .NET Core.

  3. (recommended) Install the .NET API analyzer into your projects to identify APIs throwing PlatformNotSupportedException on some platforms and some other potential compatibility issues.

  4. Convert all of your packages.config dependencies to the PackageReference format with the conversion tool in Visual Studio.

  5. Create new projects for .NET Core and copy over source files, or attempt to convert your existing project file with a tool.

  6. Port your test code.

like image 57
pushkin Avatar answered Oct 25 '22 01:10

pushkin


Most of BCL is still the same API-wise, so conversion is definitely viable for consideration. Yes, there may be incompatibilities in your code (or more often - with your dependencies) and the easiest way to check is to try building it with .net core.

For more details about when to convert (and when to rewrite) or about options of performing the conversion you could follow this guide: Upgrading to .NET Core and .NET Standard Made Easy.

like image 5
Imre Pühvel Avatar answered Oct 25 '22 01:10

Imre Pühvel


The easiest way to switch a .net framework project to a .netcore project is to open the csproj file and change the TargetFramework from something like this

<TargetFramework>net462</TargetFramework>

to something like this

<TargetFramework>netcoreapp3.1</TargetFramework>

You could also change it to .net standard, in case you want compatibility between .net core and .net framework consumer projects, by changing it to this:

<TargetFramework>netstandard2.0</TargetFramework>

You could target multiple frameworks like so:

<TargetFrameworks>net462;netstandard2.0</TargetFrameworks> 

Ensure you use the correct version number and obviously depending on what this project already targets, things are going to break and will need fixing. For example, you can't use a .net framework class library with a .net core project.

A more detailed process is provided here: https://learn.microsoft.com/en-us/dotnet/core/porting/

like image 1
Ash Avatar answered Oct 25 '22 03:10

Ash