自定义 Nuget 包显示缺少依赖项错误

Custom Nuget package shows missing dependency error

我在一个解决方案 (.NET Core) 中有两个 class 库。其中一个(cl1)是一个主库,它依赖于另一个库(cl2)。我在项目文件夹(与 .csproj 文件相同的位置)中为 cl1 项目添加了一个仅包含所需元数据(无依赖项,无文件)的 .nuspec 文件,并且我已设置 GeneratePackageOnBuild 属性 到 true。 每当我构建 class 库 (cl1) 时,都会在 debug/release 文件夹中自动创建 .nupkg

当我检查生成的 .nupkg 文件时,我看到两件奇怪的事情:

  1. 生成的.nuspec文件与我在项目文件夹中添加的文件不同
  2. cl2 在新生成的 .nuspec 文件中被提及为依赖项,但是 cl2 的 DLL 不包含在 lib 文件夹中18=]。因此,每当我在另一个解决方案中使用此包时,我都会收到 cl2.
  3. 的错误 No packages exist with this id in source(s)

我已经上网了,但是没有找到解决上述错误的正确方法。

And I have added a .nuspec file [...] in the project folder(same location of .csproj file)

您必须使用 NuspecFile 标记在 .csproj 中指定您自己的 NuSpec 文件的路径,否则它将被忽略,并且将使用 [=] 中的元数据创建包11=] 文件,参见 reference。您需要使用文件的相对路径或绝对路径,例如:

<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <NuspecFile>cl1.nuspec</NuspecFile>
   </PropertyGroup>
</Project>
  1. The generated .nuspec file is different than what I have added in the project folder

如前所述,您的 NuSpec 文件可能不包含在内。然而,即使是,也可能存在差异,因为一些信息,例如源文件位置是不必要的,目标位置在大多数情况下由内部包文件结构本身给出,所以它不存在是因为它是多余的。

  1. cl2 is mentioned as a dependency in the newely generated .nuspec file, but the dll for the cl2 is not included in the lib folder of the .nupkg. So, whenever I consume this nupkg in other solution, I am getting error " No packages exist with this id in source(s)" for the cl2.

Dependencies 用于 packages。因此,当 NuGet 恢复包时,它会搜索此包所依赖的其他包,此处为 cl2,但存在 none,因此出现错误。打包项目时,引用的项目包含在包中。那是一个开放的issue and there are workarounds,你可以试试。

最可靠但不方便的解决方案是完全避免该问题。

  • 只使用一个项目,所有内容都包含在包中
  • 单独打包每个项目并使用生成的包而不是引用的项目