【问题标题】:How can I specify different dependencies for different versions of the .NET framework in a custom NuGet package?如何在自定义 NuGet 包中为不同版本的 .NET 框架指定不同的依赖项?
【发布时间】:2016-02-10 14:47:52
【问题描述】:

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

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

这是我的 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 的项目时,这些依赖项也会被引入。这是我不想要的。任何帮助表示赞赏。

【问题讨论】:

    标签: c# .net nuget


    【解决方案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>
    

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

    【讨论】:

    • 是的。丑陋之处在于,如果您没有明确指定 .NET 4.5,它会假定应该选择 .NET 4 设置。如果您检查规范,许多类似的继承。
    • 现在如果包 X 在 1.0.0.0 处使用包 Y 而包 Z 在 2.0.0.0 处使用包 Y,现在该怎么做?现在正在努力解决这个问题,因为看起来两个包使用不同版本的 log4net
    猜你喜欢
    • 2019-02-24
    • 2018-11-01
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多