与 NuGet 捆绑的本机 dll 的输出目录 [英] Output Directory of native dll bundled with NuGet

查看:98
本文介绍了与 NuGet 捆绑的本机 dll 的输出目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个包含本机 DLL 的 NuGet 包,当项目使用该包时,这些 DLL 将放置在输出文件夹中.我尝试使用 这个问题 中的几个建议,但我总是遇到同样的问题.

I am trying to build a NuGet package that includes native DLLs which are to be placed in the output folder when a project uses the package. I have tried to use the several suggestions from this question, but I am always running in the same problem.

我目前的 NuGet 包布局是这样的:

My current NuGet package layout is like this:

\build
  packageId.targets
  file1.dll
  file2.dll
\lib
  \netstandard1.4
    assembly.dll

packageId.targets 的内容是:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <NativeLibs Include="$(MSBuildThisFileDirectory)\*.dll"/>
    <None Include="@(NativeLibs)" Link="$(RecursiveDir)$(Filename)$(Extension)">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

根据其他问题的答案,这应该会导致我的 DLL 被放置在使用包的项目的 bin\Debug 目录中.然而,他们不是.相反,它们被放置在 bin\Debug\packages\packageId\build 中.

This should, according to the answers of the other questions, lead to my DLLs being placed in the bin\Debug directory of the project using the package. However, they are not. Instead, they are placed in bin\Debug\packages\packageId\build.

现在我已经尝试了很多,我注意到越来越多我无法理解的奇怪行为:

Now I have experimented a lot, and I noticed more and more strange behavior which I cannot make any sense of:

  • 如果我将 DLL 移动到 NuGet 包的根目录(就像一个答案所建议的那样)并相应地更改 .targets 文件,它们根本不会被复制.也没有错误消息.
  • 如果我将 .targets 文件更改为 only 引用 file1.dllInclude=Link=,无论如何都会复制这两个文件.
  • 所以我想知道是否某些策略只是忽略了 .targets 文件并将 build 中的任何内容复制到输出文件夹中的该路径,但是当我删除 .targets 文件,DLL 文件将不再被复制.
  • If I move the DLLs to the root of the NuGet package (like one answer suggests) and change the .targets file accordingly, they are not copied at all. There also is no error message.
  • If I change the .targets file to only reference file1.dll in both Include= and Link=, both files get copied anyway.
  • So I wondered if some policy just ignores the .targets file and copies whatever is in build to that path in the output folder, but when I remove the .targets file, the DLL files will not get copied anymore.

现在我更不明白发生了什么.

Now I understand even less what's happening.

我需要更改什么才能将 DLL 直接复制到 bin\Debug 中?

What do I need to change to get the DLLs copied right into bin\Debug?

推荐答案

在 NuGet 中处理特定于运行时的同意的新方法是使用 runtimes 文件夹来放置本机资产:

The new way to handle runtime-specific assents in NuGet is to use the runtimes folder to place native assets:

\lib
  \netstandard2.0
    ManagedWrapper.dll
\runtimes
  \win-x86
    \native 
     NativeThing.dll
  \win-x64
    \native 
     NativeThing.dll
  \linux-x64
    \native 
     libNativeThing.so
  \osx-x64
    \native 
     libNativeThing.dylib

如果包是从 .NET Framework 项目使用的,您可能需要添加对 Microsoft.NETCore.Platforms 包的引用,该包提供运行时图 (runtimes.json) 以便 NuGet 在不使用基本 RID 的情况下提供正确的 RID 映射(例如 win10-x64 回退到 win-x64 资源).

If the package is consumed from a .NET Framework project, you may need to add a reference to the Microsoft.NETCore.Platforms package wich provides the runtime graph (runtimes.json) for NuGet to provide proper RID mappings if you don't use base RIDs (e.g. win10-x64 falls back to win-x64 resources).

这篇关于与 NuGet 捆绑的本机 dll 的输出目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆