A code that supports both .NET Framework and .NET Core

Transfer: https: //www.cnblogs.com/tianqing/p/11614303.html

 

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>

我们重点编辑TargetFramework这个节,改为TargetFrameworks,例如:

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

保存后,会提示:

  全部重新加载后,新的Project的依赖项是这样的:

   

这样,这个Project就支持了多个.NET 目标框架,我们可以面向不同的.NET 目标框架添加不同的引用,当然如果依赖的Nuget也同时支持相同的.NET 目标框架,那就最匹配了:例如:Newtonsoft.Json

添加Nuget引用后,Project在不同的.NET 目标框架的引用是这样的:

当然,我们可以为单独为指定的.NET 目标框架添加不同的引用,例如:

复制代码
 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>
复制代码

 

参考链接:https://docs.microsoft.com/en-us/dotnet/standard/frameworks

二、. 代码中使用预处理指令同时支持.NET Framework 和 .NET Core

如果同一块业务逻辑,在.NET Framework和.NET Core实现不一样,我们在同一个代码中,如果通过预处理指令实现:

复制代码
 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         }
复制代码

参考链接:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if

这个地方有个对照表:

这样,代码写完后,编译一下,可以看到有两个文件夹生成:

复制代码
1>------ 已启动全部重新生成: 项目: LibNetCore, 配置: Debug Any CPU ------
1>C:\Program Files\dotnet\sdk\3.0.100-preview3-010431\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(151,5): message NETSDK1057: 你正在使用 .NET Core 的预览版。请查看 https://aka.ms/dotnet-core-preview
1>LibNetCore -> C:\Users\zhougq\source\repos\LibNetCore\bin\Debug\netstandard2.0\LibNetCore.dll
2>------ 已启动全部重新生成: 项目: TestLibrary, 配置: Debug Any CPU ------
2>C:\Program Files\dotnet\sdk\3.0.100-preview3-010431\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(151,5): message NETSDK1057: 你正在使用 .NET Core 的预览版。请查看 https://aka.ms/dotnet-core-preview
2>TestLibrary -> C:\Users\zhougq\source\repos\TestLibrary\bin\Debug\netstandard2.0\TestLibrary.dll
2>TestLibrary -> C:\Users\zhougq\source\repos\TestLibrary\bin\Debug\net451\TestLibrary.dll
========== 全部重新生成: 成功 2 个,失败 0 个,跳过 0 个 ==========
复制代码

三. 编译生成两个.NET框架的Dll,制作支持多个.NET目标框架的Nuget包

 上个步骤中生成的两个.NET 目标版本的dll,可以分别制作支持多个.NET 目标框架的Nuget包。

 右键Project属性设置中,可以设置Nuget打包

 

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

  使用PackageExplorer编辑生成好的Nuget包:

 

  以上就是本次的.NETCore 技术分享。

Guess you like

Origin www.cnblogs.com/gt1987/p/12103089.html