Research .NetCore technology - a set of code that supports both .NET Framework and .NET Core

During the migration .NET Core, we will migrate existing .NET Framework code to the .NET Core. If only a small online application Fortunately, the migration after the upgrade is complete, only you need to maintain .NET Core this version of the code.

However, if a large-scale distributed applications, hundreds Server, thousands of .NET application process. In this scenario, within a certain period of time, we need to maintain two sets of .NET Framework and .NET Core Code, the same product

Characteristics, respectively, in the two sets of code need to realize, this code synchronization workload is very large. Therefore, in this scenario, it is necessary to use the same set of code that supports both .NET Framework and support for .NET Core.

With this demand scenario, we started research .NET Core technology today to share. First we summarize the whole idea:

1. Project engineering level support multiple target frame, add different references for different target .NET Framework

2. Using preprocessing directives code supports .NET Framework and .NET Core

3. Dll compiled two .NET framework, a plurality of package production support Nuget target .NET Framework

We look at the first step:

1. In Project engineering level support multiple target framework, aimed at different target .NET Framework add different references

In this sample code, we use .NET Standard 2.0 Class Library Project. Target framework supports .NET Framework 4.5.1 and .NET Standard 2.0

 

 

 Double-click Project, XML file into the editing mode

1 <Project Sdk="Microsoft.NET.Sdk">
2   <PropertyGroup>
3     <TargetFramework>netstandard2.0</TargetFramework>
4   </PropertyGroup>
5 </Project>

We focus TargetFramework edit this section instead TargetFrameworks, such as:

1  <PropertyGroup>
2    <TargetFrameworks>netstandard2.0;net451</TargetFrameworks>
3   </PropertyGroup>

Once saved, you will be prompted:

  After a full reload, the new Project dependencies like this:

   

In this way, the Project will support a number of targets .NET framework, we can face different target .NET Framework add different references, of course, if dependent Nuget also support the same target .NET framework, it would match the most: for example, : Newtonsoft.Json

After adding Nuget references, Project references in different .NET target framework is this:

Of course, we can separate different references to add to the specified target .NET Framework, such as:

 1 <Project Sdk="Microsoft.NET.Sdk">
 2 
 3   <PropertyGroup>
 4     <TargetFrameworks>netstandard2.0;net451;</TargetFrameworks>
 5   </PropertyGroup>
 6 
 7   <ItemGroup>
 8     <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
 9   </ItemGroup>
10 
11   <ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
12     <ProjectReference Include="..\LibNetFramework\LibNetFramework.csproj" />
13   </ItemGroup>
14   
15   <ItemGroup  Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
16     <ProjectReference Include="..\LibNetCore\LibNetCore.csproj" />
18   </ItemGroup>
19 
20 </Project>

 

Reference Links: https://docs.microsoft.com/en-us/dotnet/standard/frameworks

II. Using preprocessing directives code supports .NET Framework and .NET Core

If the same piece of business logic, in the .NET Framework and .NET Core achieved is not the same, we have the same code, if implemented by preprocessing directives:

 1  public string UserID
 2         {
 3             get
 4             {
 5 #if NET451
 6                 return Convert.ToString(HttpContext.Current.Session["UserID"]);
 7 #elif NETSTANDARD2_0
 8                 return httpContext.Session.GetString("UserID");
 9 #endif
10             }
11             private set
12             {
13 #if NET451
14                 HttpContext.Current.Session["UserID"] = value;
15 #elif NETSTANDARD2_0
16                 httpContext.Session.SetString("UserID", value);  
17 #endif
18             }
19         }

Reference Links: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if

This place has a comparison table:

In this way, the code is finished, compile it, you can see there are two folders generated:

1 > ------ Rebuild All started: Project: LibNetCore, Configuration: Debug the CPU the Any ------
 1 > C: \ Program Files \ DOTNET \ sdk \ 3.0 . 100 -preview3- 010 431 \ sdks \ Microsoft.NET.Sdk \ Targets \ Microsoft.NET.RuntimeIdentifierInference.targets ( 151 , 5 ): the Message NETSDK1057: you are using .NET Core's preview. See the HTTPS: // aka.ms/dotnet-core-preview 
1 > LibNetCore -> C: \ the Users \ zhougq \ Source \ Repos \ LibNetCore \ bin \ De bug \ netstandard2.0 \ LibNetCore.dll
 2 > --- --- Rebuild all started: project: TestLibrary, configuration: Debug the CPU ------ the Any
 2 > C: \ program Files \ DOTNET \ sdk \ 3.0 . 100 -preview3-010 431 \ sdks \ Microsoft.NET.Sdk \ Targets \ Microsoft.NET.RuntimeIdentifierInference.targets ( 151 , 5 ): the Message NETSDK1057: You are using .NET Core's preview. See the HTTPS: // aka.ms/dotnet-core-preview 
2 > TestLibrary -> C: \ the Users \ zhougq \ Source \ Repos \ TestLibrary \ bin \ Debug \ netstandard2.0 \ TestLibrary.dll
 2 > TestLibrary -> C : \ the Users \ zhougq \ Source \ Repos \ TestLibrary \ bin \ Debug \ net451 \ TestLibrary.dll
 ========== Rebuild all: success 2 Ge, a failure 0 Ge, skipped 0 Ge === =======

III. Dll compiled two .NET framework, a plurality of production support .NET Framework target packet Nuget

 Generated in the previous step two .NET target version dll, respectively, can be prepared to support a plurality of packages Nuget target .NET Framework.

 Right Project property settings, you can set Nuget package

 

   编译工程:Successfully created package 'C:\Users\zhougq\source\repos\TestLibrary\bin\Debug\TestLibrary.1.0.0.nupkg'.

  Use PackageExplorer edit generate good Nuget package:

 

  Those are the times .NETCore technology sharing.

 

Zhou Guoqing

2019/9/30

   

Guess you like

Origin www.cnblogs.com/tianqing/p/11614303.html