C#

从.Net Framework 迁移到 .Net Core

try-convert 工具可以帮助我们迁移工程

Prerequisites

Microsoft launched a dedicated extension to visualize or check if your application is portable to the other .Net Frameworks.

Check the below article on how to install and use the .Net Portability Analyzer.Is your application Portable?.NET Portability Analyzer is a new tool to check application flexibility across various .NET platforms and how much…medium.com

Getting Started

Create a simple .Net Framework HelloWorld application from Visual Studio or Download the sample Github projectssukhpinder/HelloWorldDotNetFrameworkNet Framework hello world console application. Contribute to ssukhpinder/HelloWorldDotNetFramework development by…github.com

=> Install .Net Core SDK Link

Install the new tool via Dotnet SDK from the command line.

dotnet tool install -g try-convert

try-convert tool options

-p, --project <p>                 
The path to a project to convert-w, --workspace <w>
The solution or project file to operate on. If a project is not specified, the command will search the current directory for one.-m, --msbuild-path <m>
The path to an MSBuild.exe, if you prefer to use that--target-framework, -tfm <tfm>
The name of the framework you would like to upgrade to. If unspecified, the default TFM chosen will be the highest available one found on your machine.--preview
Use preview SDKs as part of conversion--diff-only
Produces a diff of the project to convert; no conversion is done--no-backup
Converts projects and does not create a backup of the originals.--keep-current-tfms
Converts project files but does not change any TFMs. If unspecified, TFMs may change.

Let’s start the conversion

Go to .Net Framework project directory where the “.csproj” file exists and open a command prompt session in Administrator mode.

Firstly, .Net Framework gets converted into .Net Standard SDK, and from there, it’s turned into .Net Core framework.

Command to convert to .Net Standard

The below command will generate a new “.csproj” file of .Net standard type, whereas keeping the backup of .Net Framework file.

Project structure before migration: Targets 4.7 version

By default, backup is enabled but overridden using no backup option.

try-convert -p HelloWorldDotNetFramework.csproj

Project structure after migration: .Net Standard

An additional “.csproj” file will be created, and the previous file is renamed.

Ifyou need to revert the migration changs, delete the newly created “.csproj” file and rename the file ending with the “.old” extension i.e., remove the “.old” extension from filename.

Convert to .Net Core Framework

Open the “.csproj” file in the editor and manually change the tagertFrameworkVersion to .Net Core version installed in local.

Save the file, run following commands in sequence.

dotnet builddotnet run

Ifnot sure about packages involved during migration, run below command to generate a detailed report before migration.

try-convert --diff-only

Thank you for reading. Keep visiting and share this in your network. Please put your thoughts and feedback in the comments section.

Follow me on LinkedIn Instagram Facebook Twitter.

发表评论

邮箱地址不会被公开。 必填项已用*标注