Search code examples
visual-studio-2012dllnugetnuget-packagetargets

Native DLLs Not Imported When Installing NuGet Package


I have a NuGet package that contains one managed DLL and a bunch of native DLLs. The managed DLL calls into the native DLLs using pinvoke. I have successfully packaged everything into a NuGet package with the following directory structure:

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

My .targets file looks like this

<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>

Everything I have described so far follows the instructions found here. From what I have read, when visual studio installs this NuGet package, it should insert an "Import Project" section in my .csproj file that reads in my .targets file and then copies all of my unmanaged DLLs into my bin\Debug directory. I have done this successfully in the past using Visual Studio 2015, but for some reason with my current setup using Visual Studio 2012 it is not working. My NuGet package gets installed and a Reference Include=Managed... section gets added to my .csproj file, but the .targets file is never Imported and my unmanaged DLLs never get copied over.

Notes:

  • My .targets file has the same name as my NuGet package ID.
  • My Visual Studio installation uses NuGet version 2.0
  • I suspect the issue has something to do with my version of Visual Studio or NuGet.

Solution

  • As I mentioned in my comment to my question, I discovered that Visual Studio 2012 which comes with NuGet version 2.0 does not support the ability to put .targets or .props files in the build directory of the NuGet package. I had to figure out how people copied over native DLLs from a NuGet package before that feature was added. Evidently it was done using a powershell script in a tools directory of the NuGet package. Here is the powershell script that successfully copied over all of my native DLLs to the output directory of my Visual Studio project:

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

    My new NuGet package directory structure now looks like this:

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