nuget - 如何使用托管项目中的 native NuGet 包?

标签 nuget nuget-package

我有一个托管项目,它通过 P/Invoke 使用 C 风格的 native DLL。

打包 native DLL 以便将其作为 NuGet 包添加到托管项目并将 DLL 自动复制到输出文件夹的正确方法是什么?

我目前使用 CoApp 为 native DLL 创建了一个包,但我无法从托管项目中使用它;尝试添加包时出现以下错误:

Could not install package 'foo.redist 1.0.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5.1', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.



目前我在 autopkg 文件中只有这些“枢轴”:
[Win32,dynamic,release] {
    bin: release\foo.dll;
}
[Win32,dynamic,debug] {
    bin: debug\foo.dll;
}

...我需要添加其他东西吗?

最佳答案

我处于类似的情况。我选择不对这个项目使用 CoApp,而是创建一个新的 nuspec/.targets 文件组合。

在 nuspec 文件中,我使用了 <files>元素来列出我的 native dll。

在 .targets 文件中,您可以访问 msbuild Condition 属性,该属性允许基本配置透视。在我们的例子中,我们总是部署 64 位二进制文​​件,因此不需要 Platform pivot,但如果需要,您也可以添加它。

我在运行 nuget pack 时收到警告,因为二进制文件不在 lib 内,但否则它可以正常工作。

脚步:

  • 运行 nuget spec在包含您的 vcxproj 的文件夹中
  • 创建 .build文件夹,在该文件夹中创建一个空 mydll.targets文件(匹配 nuspec 文件名)
  • 类似于下面的示例手动填充文件;

  • 示例 mydll.nuspec:
    <?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
      ...your metadata here
    </metadata>
    <files>
      <file src="x64\Release\my.dll" target="x64\Release\my.dll" />
      <file src="x64\Debug\my.dll" target="x64\Debug\my.dll" />
    </files>
    </package>
    

    示例 mydll.targets:
    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
    <None Include="$(MSBuildThisFileDirectory)\..\x64\Release\my.dll" Condition="'$(Configuration)'=='Release'">
      <Link>my.dll</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Include="$(MSBuildThisFileDirectory)\..\x64\Debug\my.dll"  Condition="'$(Configuration)'=='Debug'">
      <Link>my.dll</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    </ItemGroup>
    </Project>
    

    关于nuget - 如何使用托管项目中的 native NuGet 包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28875363/

    相关文章:

    powershell - nuget 对文件名中的空格进行编码

    nuget-package - NuGet - 找不到 'Add Library Package Reference...'

    git - 找不到文件 packages.config

    nuget - 在 C :\Users\username\. nuget\packages\中找不到包。运行 NuGet 包还原以下载包

    c# - 在 Nuget 的后期构建中获取版本

    Angular 4 Nuget 包

    c# - NuGet 包引用复制 dll 本地

    c# - 如何连接MySQL数据库?

    visual-studio - 安装 NuGet 包时如何更改 packages.config 位置

    .net - NuGet "TargetFramework"缺少 .Net 引用编译错误...如何修复?