简体   繁体   English

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

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

I'm trying to create a NuGet package which has a dependency on System.Net.Http (need the HttpClient). 我正在尝试创建一个依赖于System.Net.Http的NuGet包(需要HttpClient)。 For framework version 4.5.1, this assembly is a part of the BCL. 对于框架版本4.5.1,此程序集是BCL的一部分。 Hoewever, in 4.0 it is not. Hoewever,在4.0中不是。 I believe it having compiling correctly with the proper conditional statements in the csproj. 我相信它已经正确编译了csproj中的正确条件语句。

The problem I'm currently wrestling with is that when I reference this package in a 4.5.1 project, it pulls in the dependency on Microsoft.Net.Http . 我目前正在努力解决的问题是,当我在4.5.1项目中引用此包时,它会依赖于Microsoft.Net.Http I really only want to depend on Microsoft.Net.Http for net40. 我真的只想依靠Microsoft.Net.Http来获取net40。

Here's my nuspec file: 这是我的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>

In VS, the NuGet package shows this: 在VS中,NuGet包显示:

.NETFrameworkVersion = V4.0

But again, I'm those dependencies are also being pulled in when using a project with target framework 4.5.1. 但同样,当使用带有目标框架4.5.1的项目时,我也会将这些依赖项纳入其中。 Which I don't want. 我不想要的。 Any help is appreciated. 任何帮助表示赞赏。

Need to be more specific when defining dependencies by framework version. 在通过框架版本定义依赖关系时需要更具体。

<?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>

两个版本的框架

Typical... after struggling with this for hours, I come up with the answer minutes after posting the question. 典型......在经历了数小时的努力之后,我在发布问题后得出了答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET — 管理不同版本的 NuGet 包的公共依赖项 - .NET — Managing NuGet packages’ common dependencies with different versions 我们可以在NuGet中添加2个不同版本的相同包 - Can we add 2 different versions of same package in NuGet 如何在MEF中处理具有相同依赖项的不同版本的模块? - How can I deal with modules with different versions of the same dependencies in MEF? Nuget package 版本分辨率不同版本 - Nuget package version resolution with different versions 使用相同 NuGet 包的两个不同版本 - Using two different versions of same the NuGet package nuget package 与其参考的 nuget 封装之间的不同版本 - Different versions between a nuget package and its referenced nuget packages Nuget.exe和NuGet程序包管理器解决了不同的依赖性 - Nuget.exe and NuGet Package Manager resolve different dependencies 对两个 nuget 包 C# .net 框架使用不同版本的包 - Use Different version of package for two nuget package C# .net framework exe和程序集可以在不同版本的.NET框架上运行吗? - Can exe and assembly run on different versions of .NET framework? .Net / VisualStudio如何处理具有相同nuget包的项目依赖项,每个项目具有不同的nuget目标 - How .Net/VisualStudio handles having projects dependencies each with different nuget target for same nuget packages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM