如何发布同时支持.net core和.net framework的nuget包

原文:https://blog.csdn.net/quxing10086/article/details/80258422

项目支持多版本的改造步骤

过程中其实没有想到,要发布一个同时支持 Net Standard 2.0 和 Net Framework 4.5 版本的 Nuget 包,还是比较繁琐的。需要将原来的两个分支的代码合并到一起,并通过预处理命令来分别编译为不同版本。

下面,简单记录一下一些重要的步骤:

  • 创建并使用新的 Net Standard 项目文件格式来创建。

  • 修改 Rafy.csproj 文件,使其支持多个 .NET 版本:
  1.  
     
  2.  
    <Project Sdk="Microsoft.NET.Sdk">
  3.  
    <PropertyGroup>
  4.  
    <TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
  • Rafy.csproj 文件中,为不同的版本添加不同的引用:
  1.  
     
  2.  
    <ItemGroup Condition="'$(TargetFramework)' == 'net45'">
  3.  
    <Reference Include="PresentationFramework" />
  4.  
    <Reference Include="System" />
  5.  
    <Reference Include="System.Configuration" />
  6.  
    <Reference Include="System.Core" />
  7.  
    <Reference Include="System.Runtime.Caching" />
  8.  
    <Reference Include="System.Runtime.Serialization" />
  9.  
    <Reference Include="System.ServiceModel" />
  10.  
    <Reference Include="System.Transactions" />
  11.  
    <Reference Include="System.Web" />
  12.  
    <Reference Include="System.Xaml" />
  13.  
    <Reference Include="System.Xml.Linq" />
  14.  
    <Reference Include="System.Data.DataSetExtensions" />
  15.  
    <Reference Include="Microsoft.CSharp" />
  16.  
    <Reference Include="System.Data" />
  17.  
    <Reference Include="System.Xml" />
  18.  
    <Reference Include="WindowsBase" />
  19.  
    <PackageReference Include="Castle.Core" Version="4.1.1" />
  20.  
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
  21.  
    </ItemGroup>
  22.  
     
  23.  
    <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
  24.  
    <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
  25.  
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.0.0" />
  26.  
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
  27.  
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
  28.  
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
  29.  
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
  30.  
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
  31.  
    <PackageReference Include="System.ComponentModel" Version="4.3.0" />
  32.  
    <PackageReference Include="System.ComponentModel.TypeConverter" Version="4.3.0" />
  33.  
    <PackageReference Include="System.Data.Common" Version="4.3.0" />
  34.  
    <PackageReference Include="Castle.Core" Version="4.1.1" />
  35.  
    <PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
  36.  
    </ItemGroup>
  • 还可以自定义一些缩写的常量:
  1.  
     
  2.  
    <PropertyGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
  3.  
    <DefineConstants>NS2</DefineConstants>
  4.  
    </PropertyGroup>

  • 修改合并后的项目中的所有相关代码,都使用预处理命令来区别不同的版本,如:
[c#] view plain copy
 
  1. <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>)  
  2.            </span>{  
  3.                <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>)  
  4.                {  
  5.    <span class="hljs-meta" style="color:rgb(43,145,175);">#<span class="hljs-meta-keyword">if</span> NET45</span>  
  6.                    _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;  
  7.                    <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();  
  8.    <span class="hljs-meta" style="color:rgb(43,145,175);">#<span class="hljs-meta-keyword">endif</span></span>  
  9.          
  10.    <span class="hljs-meta" style="color:rgb(43,145,175);">#<span class="hljs-meta-keyword">if</span> NS2</span>  
  11.                    <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>);  
  12.                    <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>)  
  13.                    {  
  14.                        <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>);  
  15.                    }  
  16.                    _section = <span class="hljs-keyword" style="color:rgb(0,0,255);">new</span> RafyConfigurationSection();  
  17.                    rafyRawSection.Bind(_section);  
  18.    <span class="hljs-meta" style="color:rgb(43,145,175);">#<span class="hljs-meta-keyword">endif</span></span>  
  19.                }  
  20.            }</code>  
  • 配置项目为编译时生成对应的 Nuget 包。

  • 生成,并发布。最终生成的 Nuget 包格式是这样的:

通过上述几步,就使得 Rafy 框架支持了 Net Standard 版本了。

原文地址:https://www.cnblogs.com/h1nson/p/9413071.html