原文:https://blog.csdn.net/quxing10086/article/details/80258422
项目支持多版本的改造步骤
过程中其实没有想到,要发布一个同时支持 Net Standard 2.0 和 Net Framework 4.5 版本的 Nuget 包,还是比较繁琐的。需要将原来的两个分支的代码合并到一起,并通过预处理命令来分别编译为不同版本。
下面,简单记录一下一些重要的步骤:
- 创建并使用新的 Net Standard 项目文件格式来创建。
- 修改 Rafy.csproj 文件,使其支持多个 .NET 版本:
-
-
<Project Sdk="Microsoft.NET.Sdk">
-
<PropertyGroup>
-
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
- Rafy.csproj 文件中,为不同的版本添加不同的引用:
-
-
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
-
<Reference Include="PresentationFramework" />
-
<Reference Include="System" />
-
<Reference Include="System.Configuration" />
-
<Reference Include="System.Core" />
-
<Reference Include="System.Runtime.Caching" />
-
<Reference Include="System.Runtime.Serialization" />
-
<Reference Include="System.ServiceModel" />
-
<Reference Include="System.Transactions" />
-
<Reference Include="System.Web" />
-
<Reference Include="System.Xaml" />
-
<Reference Include="System.Xml.Linq" />
-
<Reference Include="System.Data.DataSetExtensions" />
-
<Reference Include="Microsoft.CSharp" />
-
<Reference Include="System.Data" />
-
<Reference Include="System.Xml" />
-
<Reference Include="WindowsBase" />
-
<PackageReference Include="Castle.Core" Version="4.1.1" />
-
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
-
</ItemGroup>
-
-
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
-
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
-
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.0.0" />
-
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
-
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
-
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
-
<PackageReference Include="System.ComponentModel" Version="4.3.0" />
-
<PackageReference Include="System.ComponentModel.TypeConverter" Version="4.3.0" />
-
<PackageReference Include="System.Data.Common" Version="4.3.0" />
-
<PackageReference Include="Castle.Core" Version="4.1.1" />
-
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
-
</ItemGroup>
- 还可以自定义一些缩写的常量:
-
-
<PropertyGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
-
<DefineConstants>NS2</DefineConstants>
-
</PropertyGroup>
- 修改合并后的项目中的所有相关代码,都使用预处理命令来区别不同的版本,如:
- <code class="hljs cs" style="vertical-align:middle;background-color:rgb(255,255,255);line-height:1.5;font-family:'Courier New', sans-serif;font-size:12px;border:1px solid rgb(204,204,204);"> <span class="hljs-function"><span class="hljs-keyword" style="color:rgb(0,0,255);">private</span> <span class="hljs-keyword" style="color:rgb(0,0,255);">void</span> <span class="hljs-title" style="color:rgb(163,21,21);">EnsureLoaded</span>(<span class="hljs-params"></span>)
- </span>{
- <span class="hljs-keyword" style="color:rgb(0,0,255);">if</span> (_section == <span class="hljs-keyword" style="color:rgb(0,0,255);">null</span>)
- {
- <span class="hljs-meta" style="color:rgb(43,145,175);">#<span class="hljs-meta-keyword">if</span> NET45</span>
- _section = ConfigurationManager.GetSection(<span class="hljs-string" style="color:rgb(163,21,21);">"rafy"</span>) <span class="hljs-keyword" style="color:rgb(0,0,255);">as</span> RafyConfigurationSection;
- <span class="hljs-keyword" style="color:rgb(0,0,255);">if</span> (_section == <span class="hljs-keyword" style="color:rgb(0,0,255);">null</span>) _section = <span class="hljs-keyword" style="color:rgb(0,0,255);">new</span> RafyConfigurationSection();
- <span class="hljs-meta" style="color:rgb(43,145,175);">#<span class="hljs-meta-keyword">endif</span></span>
- <span class="hljs-meta" style="color:rgb(43,145,175);">#<span class="hljs-meta-keyword">if</span> NS2</span>
- <span class="hljs-keyword" style="color:rgb(0,0,255);">var</span> rafyRawSection = ConfigurationHelper.Configuration.GetSection(<span class="hljs-string" style="color:rgb(163,21,21);">"rafy"</span>);
- <span class="hljs-keyword" style="color:rgb(0,0,255);">if</span> (rafyRawSection == <span class="hljs-keyword" style="color:rgb(0,0,255);">null</span>)
- {
- <span class="hljs-keyword" style="color:rgb(0,0,255);">throw</span> <span class="hljs-keyword" style="color:rgb(0,0,255);">new</span> InvalidProgramException(<span class="hljs-string" style="color:rgb(163,21,21);">"配置文件中没有 rafy 配置节,请检查配置文件。"</span>);
- }
- _section = <span class="hljs-keyword" style="color:rgb(0,0,255);">new</span> RafyConfigurationSection();
- rafyRawSection.Bind(_section);
- <span class="hljs-meta" style="color:rgb(43,145,175);">#<span class="hljs-meta-keyword">endif</span></span>
- }
- }</code>
- 配置项目为编译时生成对应的 Nuget 包。
- 生成,并发布。最终生成的 Nuget 包格式是这样的:
通过上述几步,就使得 Rafy 框架支持了 Net Standard 版本了。