c# - Nuget 包创建 - 面向 .NET Framework 4.6.1 的类库无法面向正确的框架

标签 c# visual-studio nuget

我创建了一个针对 .net Framework 4.6.1 的类库(几乎是一个空白 Canvas ,只有一个方法返回字符串,仅用于测试目的)。我想把它做成一个 nuget 包。我正在关注这篇文章https://learn.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-visual-studio-net-framework ,但是当我进入“nuget pack”时,我收到以下警告:

"WARNING: NU5128: Some target frameworks declared in the dependencies group of the nuspec and the lib/ref folder do not have exact matches in the other location. Consult the list of actions below:
- Add a dependency group for .NETFramework4.6.1 to the nuspec"

我尝试将依赖项组添加到 .nuspec 文件中:

<?xml version="1.0"?> 
<package>
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>Author</authors>
    <owners>$author$</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>H</description>
    <copyright>Copyright 2019</copyright>
    <tags>blah</tags>
    <dependencies>
      <group targetFramework=".NETFramework4.6.1" />
    </dependencies>   
  </metadata> 
</package>

我也尝试过:

<?xml version="1.0"?> 
<package>
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>Author</authors>
    <owners>$author$</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>H</description>
    <copyright>Copyright 2019</copyright>
    <tags>blah</tags>
    <dependencies>
      <group targetFramework="net461" />
    </dependencies>   
  </metadata> 
</package>

我仍然遇到同样的错误。

我尝试使用旧版本的 nuget,其中未显示警告消息,但同样的问题仍然存在(如果我尝试通过包管理器添加,它会说它没有依赖项)。

最佳答案

WARNING: NU5128: Some target frameworks declared in the dependencies group of the nuspec and the lib/ref folder do not have exact matches in the other location. Consult the list of actions below.

这是 Github/Nuget 中的一个未决问题,请参阅 #8713 。我可以在使用 Nuget V5.3 的 VS2017 中重现相同的问题,如果我使用 Nuget V5.2 或更早版本,这个问题就会消失。对于这种情况,恐怕您必须使用早期版本的 Nuget.exe,直到团队发布修复程序。

If I try to add via the package manager, it says it has no dependencies

1.尽管您的包项目依赖于某些 nuget 包,但如果显示 no package dependency,请检查您是否使用 packageReference 来管理当前项目中的 nuget 。目前,nuget pack 命令不适用于 .net framework projects with packageReferencenew SDK format projects

您可以通过Leo's answer查看详细信息来解决这个问题。

2.如果您所说的无依赖项是指当一个项目使用您的包时,它不会显示您的项目所针对的框架,如下所示:

enter image description here (我们可以看到您的包依赖于 Newtonsoft.Json,但我们找不到它的目标框架(net461)。)

为此,我们需要在打包时使用像 nuget pack xx.nuspec 这样的命令将 dependency+group 添加到包中。然后我们就可以看到目标框架和依赖包,如下所示:

enter image description here

希望以上内容对您有所帮助,如果我有任何误解,请随时纠正我:)

关于c# - Nuget 包创建 - 面向 .NET Framework 4.6.1 的类库无法面向正确的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58765508/

相关文章:

c++ - 增加C++中的时间分辨率

nuget - NuGet可以编辑配置文件还是只能添加配置文件?

nuget - 获取 Nuget 包的发布日期

c# - CMake CSharp 引用 nuget 包

c# - 查询 Access DB 时条件表达式中的数据类型不匹配

javascript - AJAX-Post 之后文件未下载

c# - 哪种访问共享点的方法更好?

c# - 为什么 ClickOnce 下载未更改的文件?

c# - 将 required 添加到 LabelFor

c# - 是否可以向 Visual Studio IntelliSense™ 下拉列表添加项目?