去年年中,Rafy 框架的源碼就已經支持了 Net Standard 2.0 版本。其開源代碼也已經上傳到 Github 中:https://github.com/zgynhqf/rafy/tree/NetStandard2.0 。可是這都只是在源碼層面支持 NS2.0,並無發佈其正式的 Nuget 包。要使用這個版本的開發者,不得不本身下載源碼進行編譯。git
最近,使用 Net Core 的開發者愈來愈多。因此咱們決定發佈一個正式的 Nuget 包,以方便 Net Core 的開發均可以方便地下載、更新 Rafy 框架進行使用。github
發佈後,開發者在 Nuget 中再搜索 Rafy 的最新版本時,就已經支持 Net Standard 2.0 了:c#
過程當中其實沒有想到,要發佈一個同時支持 Net Standard 2.0 和 Net Framework 4.5 版本的 Nuget 包,仍是比較繁瑣的。須要將原來的兩個分支的代碼合併到一塊兒,並經過預處理命令來分別編譯爲不一樣版本。併發
下面,簡單記錄一下一些重要的步驟:框架
建立並使用新的 Net Standard 項目文件格式來建立。插件
修改 Rafy.csproj 文件,使其支持多個 .NET 版本:3d
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<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>
private void EnsureLoaded() { if (_section == null) { #if NET45 _section = ConfigurationManager.GetSection("rafy") as RafyConfigurationSection; if (_section == null) _section = new RafyConfigurationSection(); #endif #if NS2 var rafyRawSection = ConfigurationHelper.Configuration.GetSection("rafy"); if (rafyRawSection == null) { throw new InvalidProgramException("配置文件中沒有 rafy 配置節,請檢查配置文件。"); } _section = new RafyConfigurationSection(); rafyRawSection.Bind(_section); #endif } }
配置項目爲編譯時生成對應的 Nuget 包。code
生成,併發布。最終生成的 Nuget 包格式是這樣的:xml
經過上述幾步,就使得 Rafy 框架支持了 Net Standard 版本了。同時,咱們還把 Rafy 中的一些其它公共插件也都支持了多版本。之後會不按期升級每個插件。blog