Igor K.
Igor K.

Reputation: 770

Trying to add non-.NET libraries to NuGet package

I have a .NET DLL that is using non-.NET DLLs. I'm trying to create a NuGet package out of this, but I don't seem to be penetrating the magic behind this. I read the NuGet reference about the <files> and <references> but that didn't really work out for me - VS kept on refusing to install the package on the grounds that "...Failed to add reference to NonNETLib1". I tried a different variant where non-.NET libs were bundled together with the .NET library in net40 folder and the nusepc file having no <files> section, but in this case even though the package installed OK, the code threw an exception in runtime, because it could not find the DLLs.

Here's the .nuspec I have:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>PackageName</id>
    <version>1.0.3</version>
    <authors>me</authors>
    <owners>some guys</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>description</description>
    <references>
        <reference file="NonNETLib1.dll" />
        <reference file="NonNETLib2.dll" />
        <reference file="NonNETLib3.dll" />
    </references>
  </metadata>
    <files>
        <file src="lib\NonNETLib1.dll" />
        <file src="lib\NonNETLib2.dll" />
        <file src="lib\NonNETLib3.dll" />
    </files>
</package>

The folder structure is as follows:

[lib]
  \- NonNETLib1.dll
  \- NonNETLib2.dll
  \- NonNETLib3.dll
  \- [net40]
        \- Net40Lib.dll

What am I missing here?

P.S. I know this is somewhat similar to this question, but the accepted answer to it didn't help much.

Upvotes: 1

Views: 2301

Answers (3)

GYBE
GYBE

Reputation: 481

I had exactly the same problem and i found a good solution for me with nuget 3.3. This way only the .net-lib will be referenced to the project and the non-.Net lib is only copied to bin folder.

<references>
   <reference file="Net.dll">
<references>

<files>
   <file src="..\bin\NonNet.dll" target="lib/452">
   <file src="..\bin\Net.dll" target="lib/452">
<files>

Upvotes: 0

swn1
swn1

Reputation: 39

There is a powershell-based toolset for packaging native code for NuGet consumption. It primarily deals with issues around integrating into native projects but it should provide the tooling you need to plug native dlls into .NET projects as well.

http://coapp.org/news/2013-04-26-Announcing-CoApp-Tools-For-NuGet.html

Upvotes: 0

thomasb
thomasb

Reputation: 6045

even though the package installed OK, the code threw an exception in runtime, because it could not find the DLLs

When you add a reference to a .Net DLL in your project, it's automatically copied to the output folder. But since your DLL is not .Net, you can't add a reference to it directly in the project, so it's not copied at all.

I would add a pre-build or post-build step in your project, like xcopy /y /f "$(ProjectDir)..\packages\NonNet.dll" "$(TargetDir)" but I suspect there is a better and cleaner way to do it.

In any case, it's not a nuget-related problem, but a more general Visual Studio project problem.


Update

It seems the consensus is to add the native DLL as an existing item (as a link) to the project, set it to content/copy if newer: Are there any better ways to copy a native dll to the bin folder?

Upvotes: 2

Related Questions