How do I migrate. NET Framework projects to. NET Core?

Source: Internet
Author: User
Tags dotnet

The company's projects have been used. NET Framework to develop Web projects. The base class libraries are currently in the. NET Framework 4.6.2 version. Caching, Logging,dependencyinjection,configuration and other infrastructure-dependent repositories have been kept in sync with the authorities, currently in 1.1.. NET core is becoming more stable and new development tools are released in March. Therefore, you plan to migrate the. NET Framework to. NET Core/strandard. The purpose is to make based on. NET-developed Web applications can run across platforms.

Divide your company's projects into basic class libraries, basic services, and application projects by application scenario. The Base Class library provides a variety of basic functions in the form of packages. The underlying service is built through a WCF project or through a Web API project. The application project is the Web MVC project. Base class libraries and basic services exist in the form of a single solution. The structure of each solution consists of one or more class library projects, one or more console projects, which are used for feature implementations, unit tests, and functional demonstrations, respectively. If all is to be migrated, then the priority should be the base services--base service--Application project. The object of this migration is the base Class library.

The base Class library is eventually released as a package through NuGet, and is currently only available for the. NET Framework framework. One of the goals of the migration is to enable the package to be referenced to the. NET Core,. NET Standard Framework project. In combination with official information, I chose a direct migration scenario. That is, the project file is converted directly to a new. NET Core-based project file. Details of the migration are described below.

1. Create a new. NET Core-based project.

First rename the existing project file *.csproj to *. Net46.csproj. Then use VS2017 to create a new. NET core-based project with a project type of class library (. NET core) or class library (. NET Standard). Note that VS2017 will prompt for a directory with the same name, so you can enter a different name when you create it, and then manually adjust it back.

2. Edit the project file to support targeting multiple target frameworks.

With VS2017 RC New Project, "Class library (. NET Core)" or "Class library (. NET Standard)", there is only one target framework by default. We can edit the project file to support targeting multiple target frameworks. This is modified if the target framework is supported for. NET Standard 1.4,. NET Core App 1.0, and. NET Framework 4.5.

<project sdk= "MICROSOFT.NET.SDK" >    <PropertyGroup>    <TargetFrameworks>netstandard1.4; Netcoreapp1.0;net45</targetframeworks>    </PropertyGroup></Project>

Note that the official documentation is available. NET supports a list of target frameworks, you can query more other target frameworks. If you are compatible with a lower version of the framework, the target framework version should not be set too high. such as "net45" can be used for versions such as the. NET Framework 4.5 ~ 4.6.2. such as "net46" can only be used for versions such as the. NET Framework 4.6 ~ 4.6.2.

3. Modify the application code-related APIs to support multiple target frameworks.

A. The API provided by the target framework is not the same. If necessary, you can add conditional compilation symbols to support different runtime versions.

The following is a list of common conditional compilation symbols.

. NET Framework 2.0-NET20
. NET Framework 3.5-NET35
. NET Framework 4.0-NET40
. NET Framework 4.5-NET45
. NET Framework 4.5.1-NET451
. NET Framework 4.5.2-NET452
. NET Framework 4.6-NET46
. NET Framework 4.6.1-NET461
. NET Framework 4.6.2-NET462
. NET Standard 1.0-NETSTANDARD1_0
. NET Standard 1.1-Netstandard1_1
. NET Standard 1.2-netstandard1_2
. NET Standard 1.3-netstandard1_3
. NET Standard 1.4-netstandard1_4
. NET Standard 1.5-netstandard1_5
. NET Standard 1.6-netstandard1_6

For an application of conditional compilation symbols, such as the following code:

usingSystem;usingSystem.IO;namespacebaza.netstandardtester{ Public classPathhelper { Public stringbasedirectory {Get;Set; }  PublicPathhelper () {#ifNET45basedirectory=AppDomain.CurrentDomain.BaseDirectory;#endif        }         Public stringGetrootedpath (stringpath) {            stringRootedpath = path??string.            Empty; if(!path.ispathrooted (Rootedpath)) {                if(string. IsNullOrEmpty (basedirectory))Throw NewArgumentNullException ("Please set the BaseDirectory property first"); Rootedpath=Path.Combine (basedirectory, Rootedpath); }            stringDirectory =Path.getdirectoryname (Rootedpath); if(!string. IsNullOrEmpty (directory) &&!directory.exists (Directory))            {directory.createdirectory (Directory); }            returnRootedpath; }    }}

Code description, Pathhelper provides the Getrootedpath method for calculating an absolute path based on a relative path. The BaseDirectory property needs to be set manually when the runtime is. NET core. When the runtime is. NET Framework 4.5, the BaseDirectory property is assigned by the constructor. Note System.AppDomain.CurrentDomain.BaseDirectory is used to get the managed program execution path, and the class AppDomain exists only in the. NET framework.

B.. NET standard is a package-based framework. When you need an API, such as IDataReader, you need to install the System.Data.Common package. If you are using the. NET Framework, you can find IDataReader under namespace System.Data without following the package. With the https://packagesearch.azurewebsites.net/tool, you can quickly locate which package an API is in.

C.. NET core-based projects, package version numbers, and other metadata are stored in *.csproj and do not use the AssemblyInfo.cs file, which can be deleted when porting. However, the. NET Framework project will continue to use the file.

4. The same solution, the reference policy between the class libraries.

When referencing a class library, be aware of compatibility issues with the target framework. For example, a class library (. NET Standard) project can be referenced by the. NET Core App, the. NET Framework, and other. NET Standard projects. This is because both the. NET Core app and the. NET Framework support the corresponding version of the. NET Standard library. The following table shows the complete set of. NET runtimes that support the. NET Standard library.

Platform Name Alias                
. NET Standard Netstandard 1.0 1.1 1.2 1.3 1.4 1.5 1.6 2.0
. NET Core Netcoreapp 1.0 VNext
. NET Framework Net 4.5 4.5.1 4.6 4.6.1 4.6.2 VNext 4.6.1
Mono/xamarin Platform VNext
Universal Windows Platform Uap 10.0 VNext
Windows Win 8.0 8.1
Windows Phone Wpa 8.1
Windows Phone Silverlight Wp 8.0

Note that when referencing a class library, the referenced class library also supports a multi-objective framework if the project is oriented towards a multi-objective framework.

5. Unit Testing

If you are using a. NET Framework class Library project to hold unit test code, you may encounter a bit of a problem. In VS2017 RC, test Explorer does not recognize these test units. By creating a new unit test project (. NET Framework), the generated *.csproj overwrites the original project file, which is recognized by the Test Manager.

5. MSBuild automatically compiles new solutions

Under Windows, press Release to configure the entire solution compilation.

" C:\Program Files (x86) \microsoft Visual Studio\2017\community\msbuild\15.0\bin\msbuild.exe " Xxx.sln/p:configuration=release; platform="anyCPU"

The Net46 and netstandard1.6 two directories are generated in the \bin\release directory under the relevant class Coogan directory.

6. Publish a package using Dotnet-nuget-push

When using vs2017 packaging, just right-click on the item you want to package, select "Pack and go" to generate the Xxx.0[.0].0.nupkg file under. \bin\debug or. \bin\release, and then upload the file. nupkg to nuget.org. This is more convenient by calling Dotnet-nuget-push to automate this publishing process.

dotnet nuget push xxx.0[.0].0.nupkg-k 4003d786-0000-4004-bfdf-c4f3e8ef9b3a-s http://customsource/

K: server's API key s: Server URL, if published to nuget.org, you can write this.

dotnet nuget push xxx.0[.0].0.nupkg-k 4003d786-0000-4004-bfdf-c4f3e8ef9b3a-s https://Www.nuget.org/api /v2/package

Typically, under Windows, Dotnet-nuget-push is written in a batch file to complete the deployment of the base Class library.

Summarize

After this scenario is migrated, the new solution and project files are eventually retained, and the old solution and project files are removed during the migration process. This will be followed by a new solution for cross-platform development. The porting of basic class libraries is described here. Porting the source code will be a challenge. For example, how does the API referenced by some source code work when it does not exist under the. NET Core framework? In addition, the porting of basic services and web MVC projects is to be deployed to Linux. will also encounter a variety of problems.

Reference Resources

1. Organize projects to support the. NET Framework and. NET Core

2. Dotnet-nuget-push

3. Packagesearch.azurewebsites.net

4.. NET Standard Library

5. Developing Libraries with cross Platform Tools

6. Target Frameworks

7. Porting to. NET Core from the. NET Framework

How do I migrate. NET Framework projects to. NET Core?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.