在不使用 .nuspec 的情况下创建引用引用的 .dll 的 nuget 包

Create nuget package that references referenced .dlls without using .nuspec

问题:顶级项目引用了 MyLibrary nuget,它引用了几个 vendor.dll 文件。 Vendor.dll 当 MyLibrary nuget 包被添加到顶级项目时,文件应该能够被顶级项目引用,但它们不是。

当我 运行 顶级项目时,我收到此错误:

FileNotFoundException: Could not load file or assembly 'Vendor.A, Culture=neutral, PublicKeyToken=b88d1754d700e49a'. The system cannot find the file specified.

供应商 .dll 文件未复制到 bin 文件夹。

我希望找到不需要我创建 .nuspec 文件的解决方案。

生成的 MyLibrary nuget 包的结构(使用 Nuget 包资源管理器观察):

lib
    net5.0-windows
        Vendor.a.dll
        Vendor.b.dll
    
    net5.0-windows7.0
        MyLibrary.dll
        

我不明白net5.0-windows7.0是从哪里来的。它不存在于下面引用的 TFM 列表中。此外,如果 net5.0-windows7.0 出于某种原因是必要的,为什么 MyLibrary.dll 存在但它所依赖的 .dll 不存在?

从 Visual Studio 2019 中查看包如下所示(供应商 dll 未出现):

Packages
    MyLibrary
        Compile Time Assemblies
            MyLibrary.dll

MyLibrary.csproj:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

  <PropertyGroup>
    <AssemblyVersion>1.0.0.1</AssemblyVersion>
    <FileVersion>1.0.0.1</FileVersion>
    <Version>1.0.0.3</Version>
  </PropertyGroup>
  

  <ItemGroup>
    <Content Include="$(OutputPath)\Vendor.*.dll">
      <Pack>true</Pack>
      <PackagePath>lib$(TargetFramework)</PackagePath>
    </Content>
  </ItemGroup>
  
  
  <ItemGroup>
    <Reference Include="Vendor.a">
      <HintPath>VendorLib\Vendor.a.dll</HintPath>
    </Reference>
    <Reference Include="Vendor.b">
      <HintPath>VendorLib\Vendor.b.dll</HintPath>
    </Reference>
  </ItemGroup>

TopLevel.csproj

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
  <OutputType>WinExe</OutputType>
  <TargetFramework>net5.0-windows</TargetFramework>
  <UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
  <PackageReference Include="MyLibrary" Version="1.0.0.3" />
</ItemGroup>

Target Framework Monikers

Possibly related issue

我也 found an issue 关于这种奇怪的行为,但仍然不知道 net5.0-windows7.0 来自哪里。由于问题仍未解决,团队不知道这是正常问题还是奇怪问题,我认为net5.0-windows7.0是wpf项目的nuget框架的特殊版本,所以你应该把你的dlls打包到nupkg这样的文件夹中。

虽然这不是最好的功能,但现在是一种解决方法。您可以继续跟踪问题以获取产品团队的解释。

或者试试我的建议:

功能一

1) 将您的 nuget 项目的 targetframwork 更改为

<TargetFramework>net5.0-windows7.0</TargetFramework>

正如团队所说,net5.0-windowsnet5.0-windows7.0 相同。但是,他们在打包成 nuget 方面对待它们的方式不同。

功能二

2)还是用<TargetFramework>net5.0-windows</TargetFramework>.

将其更改为:

<ItemGroup>
    <Content Include="$(OutputPath)\Vendor.*.dll">
        <Pack>true</Pack>
        <PackagePath>lib$(TargetFramework)7.0</PackagePath>
    </Content>
</ItemGroup>

此外,当你打包完nuget项目后,请delete nuget caches first或删除C:\Users\xxx\.nuget\packages下的所有文件,然后安装新发布版本的nuget打包到主工程中。

vendor 是我的自定义 nuget 项目名称。