karthikraja
karthikraja

Reputation: 497

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.

Upvotes: 41

Views: 85730

Answers (4)

John Sloan
John Sloan

Reputation: 31

I just ran into the same error that you saw (as per your comment to @ImrePühvel) when I was trying to migrate a CLI project from .NET Framework to netcoreapp3.1:

"The expression "[Microsoft.Build.Utilities.ToolLocationHelper]::GetPathToStandardLibraries(_, netcoreapp3.1, '', x64, '', '')" cannot be evaluated. Input string was not in a correct format. C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets"

In my case, it was due to a misreading of the instructions.

The old framework had a tag:

<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>

This needs to be changed to:

<TargetFramework>netcoreapp3.1</TargetFramework>

NOT

<TargetFrameworkVersion>netcoreapp3.1</TargetFrameworkVersion>

I had simply changed v4.5 --> netcoreapp3.1 in the TargetFrameworkVersion tag without changing the tag name to TargetFramework.

So double-check that you changed:

<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>

to

<TargetFramework>netcoreapp3.1</TargetFramework>

(or whatever .NET Core version you want)

and NOT:

<TargetFrameworkVersion>netcoreapp3.1</TargetFrameworkVersion>

Upvotes: 3

Ash
Ash

Reputation: 6035

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/

Upvotes: 3

pushkin
pushkin

Reputation: 10199

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.

Upvotes: 19

Imre P&#252;hvel
Imre P&#252;hvel

Reputation: 4994

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.

Upvotes: 5

Related Questions