發布 Rafy .NET Standard 版本 Nuget 包


**img **

去年年中,Rafy 框架的源碼就已經支持了 Net Standard 2.0 版本。其開源代碼也已經上傳到 Github 中:https://github.com/zgynhqf/rafy/tree/NetStandard2.0 。但是這都只是在源碼層面支持 NS2.0,並沒有發布其正式的 Nuget 包。要使用這個版本的開發者,不得不自己下載源碼進行編譯。

最近,使用 Net Core 的開發者越來越多。所以我們決定發布一個正式的 Nuget 包,以方便 Net Core 的開發都可以方便地下載、更新 Rafy 框架進行使用。

發布后,開發者在 Nuget 中再搜索 Rafy 的最新版本時,就已經支持 Net Standard 2.0 了:

項目支持多版本的改造步驟

過程中其實沒有想到,要發布一個同時支持 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>

  • 修改合並后的項目中的所有相關代碼,都使用預處理命令來區別不同的版本,如:
           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 包。

  • 生成,並發布。最終生成的 Nuget 包格式是這樣的:

通過上述幾步,就使得 Rafy 框架支持了 Net Standard 版本了。同時,我們還把 Rafy 中的一些其它公共插件也都支持了多版本。以后會不定期升級每一個插件。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM