【问题标题】:Create nuget package that references referenced .dlls without using .nuspec创建引用引用的 .dll 而不使用 .nuspec 的 nuget 包
【发布时间】:2021-05-29 01:25:51
【问题描述】:

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

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

FileNotFoundException:无法加载文件或程序集“Vendor.A,Culture=neutral,PublicKeyToken=b88d1754d700e49a”。系统找不到指定的文件。

供应商 .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

Similar question

Similar question requiring nuspec

Similar question requiring nuspec

Possibly related issue

【问题讨论】:

    标签: visual-studio msbuild nuget nuget-package .net-5


    【解决方案1】:

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

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

    或者试试我的建议:

    功能一

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

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

    正如团队所说,net5.0-windowsnet5.0-windows7.0 相同。但是,它们在打包成 nuget 时会有所不同。

    功能二

    2) 仍然使用&lt;TargetFramework&gt;net5.0-windows&lt;/TargetFramework&gt;

    将其更改为:

    <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 项目名称。

    【讨论】:

    • 我不确定我做对了,但是当我在 TargetFrameworks 元素中将 'net5.0-windows' 更改为 'net5.0-windows7.0' 时,我无法得到它编译。我不确定,但我认为您的 #2 类型更适合多目标项目。
    猜你喜欢
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 2015-10-13
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多