如何在自定义 NuGet 包中为不同版本的 .NET 框架指定不同的依赖项?

How can I specify different dependencies for different versions of the .NET framework in a custom NuGet package?

我正在尝试创建一个依赖于 System.Net.Http 的 NuGet 包(需要 HttpClient)。对于框架版本 4.5.1,此程序集是 BCL 的一部分。然而,在 4.0 中它不是。我相信它已经使用 csproj 中的正确条件语句进行了正确编译。

我目前正在努力解决的问题是,当我在 4.5.1 项目中引用这个包时,它引入了对 Microsoft.Net.Http 的依赖。对于 net40,我真的只想依赖 Microsoft.Net.Http

这是我的 nuspec 文件:

<?xml version="1.0"?>
<package>
  <metadata>
    <id>MyApp</id>
    <version>$version$</version>
    <title>MyApp</title>
    <authors>Me</authors>
    <owners>Me</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Description</description>
    <releaseNotes>Initial release</releaseNotes>
    <copyright>Copyright 2016</copyright>
    <dependencies>
      <group>
        <dependency id="Newtonsoft.Json" version="8.0.2"/>
      </group>
      <group targetFramework="net40">
        <dependency id="Microsoft.Bcl" version="1.1.10" />
        <dependency id="Microsoft.Bcl.Build" version="1.0.14" />
        <dependency id="Microsoft.Net.Http" version="2.2.29" />
      </group>
    </dependencies>
  </metadata>
  <files>
    <file src="bin\release\**\MyApp.dll" target="lib" />
  </files>
</package>

在 VS 中,NuGet 包显示如下:

但是,我再次强调,当使用带有目标框架 4.5.1 的项目时,这些依赖项也会被引入。我不想要的。感谢任何帮助。

在按框架版本定义依赖项时需要更具体。

<?xml version="1.0"?>
<package>
  <metadata>
    <id>MyApp</id>
    <version>$version$</version>
    <title>MyApp</title>
    <authors>Me</authors>
    <owners>Me</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Description</description>
    <releaseNotes>Initial release</releaseNotes>
    <copyright>Copyright 2016</copyright>
    <dependencies>
      <group targetFramework="net451">
        <dependency id="Newtonsoft.Json" version="8.0.2"/>
      </group>
      <group targetFramework="net40">
        <dependency id="Newtonsoft.Json" version="8.0.2"/>
        <dependency id="Microsoft.Bcl" version="1.1.10" />
        <dependency id="Microsoft.Bcl.Build" version="1.0.14" />
        <dependency id="Microsoft.Net.Http" version="2.2.29" />
      </group>
    </dependencies>
  </metadata>
  <files>
    <file src="bin\release\**\MyApp.dll" target="lib" />
  </files>
</package>

典型...经过几个小时的努力,我在发布问题后几分钟就得出了答案。