安装NuGet包时未导入本机DLL



我有一个NuGet包,它包含一个托管DLL和一堆本机DLL。托管DLL使用pinvoke调用本机DLL。我已经成功地将所有内容打包到NuGet包中,其目录结构如下:

lib
Managed.dll
build
Unmanaged1.dll
Unmanaged2.dll
MyNuGetID.targets

我的.targets文件看起来像这个

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<NativeLibs Include="$(MSBuildThisFileDirectory)*.*" />
<None Include="@(NativeLibs)">
<Link>%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

到目前为止,我所描述的一切都遵循这里的说明。据我所知,当visual studio安装这个NuGet包时,它应该在我的.csproj文件中插入一个"导入项目"部分,该部分读取我的.targets文件,然后将我所有的非托管DLL复制到我的binDebug目录中。我过去曾使用Visual Studio 2015成功地完成过这项工作,但由于某些原因,我目前使用Visual Studio 2012进行的设置无法正常工作。我的NuGet包被安装,Reference Include=Managed...节被添加到我的.csproj文件中,但.targets文件从未被Imported,我的非托管DLL也从未被复制。

注:

  • 我的.targets文件与我的NuGet包ID同名
  • 我的视觉Studio安装使用NuGet 2.0版
  • 我怀疑这个问题与我的Visual Studio或NuGet版本有关

正如我在对问题的评论中提到的,我发现NuGet 2.0版附带的Visual Studio 2012不支持在NuGet包的构建目录中放置.targets或.props文件。在添加该功能之前,我必须弄清楚人们是如何从NuGet包中复制本地DLL的。显然,它是使用NuGet包的工具目录中的powershell脚本完成的。以下是powershell脚本,它成功地将我的所有本机DLL复制到了我的Visual Studio项目的输出目录:

#File: toolsInstall.ps1
param($installPath, $toolsPath, $package, $project)
$nativeBinDirectory = Join-Path $installPath "build"
$projectRoot = $project.Properties.Item("FullPath").Value
$binDirectory = Join-Path $projectRoot "binDebug"
Copy-Item $nativeBinDirectory* $debugDirectory

我的新NuGet包目录结构现在看起来是这样的:

lib
Managed.dll
build
Unmanaged1.dll
Unmanaged2.dll
tools
Install.ps1

最新更新