Convert .NET Framework projects to .NET Standard projects

Nowadays, .NET  Core  is the mainstream of future development (at least until .NET 5 is released), and if we want to transform a project into a .NET  Core  project, the projects it references should first be transformed into .NET Standard projects.

The following table gives the minimum version ( reference ) that each platform supports for a given version of .NET Standard:

A brief explanation: .NET Standard 2.0 supports .NET Core 2.0 and .NET  Framework  4.6.1 (preferably 4.7.2 or higher), so there is no need for multi-platform configuration when using these versions. .NET Standard 2.1 has been incompatible with the .NET Framework since the beginning. In this case, multi-platform configuration is still required. (About multi-platform configuration, we will mention it later)

Closer to home, how do we convert a Framework class library project into a Standard class library project? The answer is to use  the dotnet try-convert  tool.

Before installation, make sure you have installed the .NET Core  SDK  (get it  here ), then search for "cmd" in the taskbar and run it as an administrator:

Use the following commands to install or uninstall:

:: Install: 
dotnet tool install -g try-convert 
:: Uninstall: 
dotnet tool uninstall -g try-convert

I've already installed:

Next, open cmd in the project path and use the following command to convert:

try-convert

Before conversion:

After conversion:

You can see that the old project file has been backed up and the new project file has been modified. At this time, we can click on the pop-up box of VS to reload the project (this operation will not be possible if VS is not opened):

Since a new project file is used, we can right-click on the project and see the "Edit Project File" option:

 

After opening it, I found that the project type is still .NET Framework 4.5.2 (net452):

Right-click on the project - Properties, you can see that the target framework is still the same as before:

Then let’s proceed to “multi-platform configuration”.

First, change TargetFramework to TargetFrameworks (add an "s") in the project file, then add "netstandard2.0" to the content inside, and separate it from the previous content with a semicolon. The modification is as follows:

Then we close VS and reopen it (reload the solution), check the project properties again, we can see that there is no specific content specified in the target framework, and there are no errors when generating it, indicating that the configuration is successful.

In addition, the dependency organization of modern projects has also changed:

If there are multiple projects in the solution that need to be converted, you can also locate the solution directory and then execute the conversion command (try-convert). The converted projects will be automatically skipped, and the remaining projects will be automatically converted in batches:

 

Through the command output, we also learned that the new project type is called ".NET SDK-style project", haha, this is exciting.

If a yellow triangle appears in a .NET Standard dependency:

You can check whether the item is needed and remove it if not. In addition, you can manually select the frame under which you are currently viewing it under the opened class file label:

If there are references that are not under the .NET Standard framework, you can right-click on "Package" - "Manage NuGet Packages":

Then search and install:

Okay, that’s about it.

Guess you like

Origin blog.csdn.net/weixin_45499836/article/details/124021985
Recommended