nuget package with multiple DLL's

2019-07-27 23:09发布

问题:

I have created a nuget package via Nuget Package Explorer with the following content.

lib
   -- CPPLib.dll
   -- DotNetWrapper.dll

CPPLib.dll is the main library with logic implementation (in native C++), and DotNetWrapper.dll is the wrapper which will be reference in C# projects.

When I try to install this nuget package, I got the following error

    Install failed. Rolling back...
    Install-Package : Failed to add reference to 'CPPLib.dll'.
    At line:1 char:16
    + install-package <<<<  -id MyPackage
    + CategoryInfo          : NotSpecified: (:) [Install-Package], InvalidOperationException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand

I know everything in "lib" folder inside the package will be installed as reference, which explains why the native CPPLib.dll can't be installed to a C# project.

Then I tried to move the CPPLib.dll into "content" folder in the package like this

   content
       -- CPPLib.dll
   lib
       -- DotNetWrapper.dll

Then the package can be installed, but the project will not build as DotNetWrapper.dll can not find CPPLib.dll in the same folder.

How can I get around this issue? Is it possible to somehow put everything in lib folder and only "expose" DotNetWrapper.dll from the package?

回答1:

you need to work with files and references in your nuspec file:

<?xml version="1.0"?>
<package  xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
    <metadata>
        ...
        <references>
            <reference file="DotNetWrapper.dll" />
        </references>  
    </metadata>
    <files>
        <file src="bin\Release\DotNetWrapper.dll" target="lib" />
        <file src="bin\Release\CPPLib.dll" target="lib" />
    </files>
</package>

so you define files to put in your package with "files", and tell the nuget that your only reference to add is wrapper DLL with "references".

Pls mention that these tags are on different level - "references" must be inside "metadata", but "files" are sibling to "metadata"