How to use NuGet native package from a managed project?

I have a managed project that uses a native C-style DLL through P / Invoke.

What is the correct way to batch build a DLL, so it can be added to the NuGet package for a managed project and copy the DLL automatically to the output folder?

Currently, I have created a package using CoApp for my native DLL, but I cannot use it from a managed project; When trying to add a package, the following error appears:

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

Currently, I only have these "anchor points" in the autopkg file:

[Win32,dynamic,release] {
    bin: release\foo.dll;
}
[Win32,dynamic,debug] {
    bin: debug\foo.dll;
}

... do I need to add something else?

+4
source share
1 answer

I am in a similar situation. I decided not to use CoApp for this project, but instead create a new combination of nuspec / .targets files.

Inside the nuspec file, I use an element <files>to display my native dlls.

.targets msbuild Condition, . 64- , Pivot , , .

nuget, 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>
+1

All Articles