38

I have a .nuspec file for my project, which references a third-party DLL that the project including my package needs to reference.

<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<releaseNotes>Locked against log4net 1.2 - less than 1.2.11 which has breaking changes</releaseNotes>
<copyright>Copyright 2012  blah blah</copyright>
<dependencies>
  <dependency id="log4net" version="[1.2,1.2.11)" />
  <dependency id="My.Other.Project" />
</dependencies>
<references>
  <reference file="Third.Party.dll" />
</references>

If I try to run nuget.exe pack My.Project.csproj from the directory with the .csproj and the .nuspec file, I get

Invalid assembly reference 'Third.Party.dll'. Ensure that a file named 'Third.Party.dll' exists in the lib directory.

I have created .\lib .\bin\Debug\lib .\obj\lib

and the file is in all three places. Where does it REALLY want the lib folder?

2 Answers 2

39

The <references> element defines the references that will be added to the project when your package is installed. What you are missing is the part that defines the files that are part of your package which is done with the <files> element. So your .nuspec file should look something like the following:

<?xml version="1.0"?>
<package>
    <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <releaseNotes>Locked against log4net 1.2 - less than 1.2.11 which has breaking changes</releaseNotes>
    <copyright>Copyright 2012  blah blah</copyright>
    <dependencies>
      <dependency id="log4net" version="[1.2,1.2.11)" />
      <dependency id="My.Other.Project" />
    </dependencies>
    <references>
      <reference file="Third.Party.dll" />
    </references>
  </metadata>
  <files>
    <file src="lib\Third.Party.dll" target="lib"/>
  </files>
</package>

The only difference is the files element after the metadata element.

2
  • 3
    In additional to this, what worked for me was to add a reference to the self project, as mentioned by Mustafakidd.
    – Yash
    Mar 18, 2016 at 8:41
  • 1
    I got hung up on the part where the <files> element has to be a peer to the <metadata> element; I tried to put <files> inside of <metadata> and it didn't like it. Careful re-reading of the file spec and then coming back to look at your post shows the right way to do it. So be careful; <files> is a peer of <metadata>
    – ggariepy
    Jun 27, 2019 at 14:29
13

I had the same problem and here's how I solved it. Seemed odd to me but it looks like it works and I don't need to create a nuget package for my third party assembly this way.

My .nuspec looks like this:

 <?xml version="1.0"?>
<package >
    <metadata>
        <id>$id$</id>
        <version>$version$</version>
        <title>$title$</title>
        <authors>$author$</authors>
        <owners>$author$</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>$description$</description>
        <releaseNotes>Some release notes</releaseNotes>
        <copyright>Copyright 2012</copyright>
        <tags>some,tags</tags>
        <references>
            <reference file="thirdparty.dll"></reference>
            <reference file="ThisAssemblyName.dll"></reference>
        </references>
    </metadata>
    <files>
        <file src="web.config.transform" target="content"/>
        <file src="lib\net40\thirdparty.dll" target="lib\net40"/>
    </files>
</package>

As you can see - I had to add a file node to copy my thirdparty dll to the target lib\net40 (in my case) folder. Then I had to add "reference" nodes for both the thirdparty.dll as well as the assembly that this NuGet package was creating. I feel like Matt Ward's solution should be correct, but it wasn't working for me until I added the (redundant) reference node.

I hope that helps!

Thanks

Mustafa

5
  • Wow, it works for me. Need to reference to current assembly. Thanks :) Jan 26, 2016 at 13:25
  • I also needed to reference the current assembly. Thanks a lot Mustafakidd.
    – Yash
    Mar 18, 2016 at 8:42
  • 1
    Based on what the documentation says, the act of adding the <references> element means assemblies in the lib folder will no longer be automatically added to the target project as references. By using that element you are saying you want to explicitly specify the references, hence why you have to specify the original assembly too. learn.microsoft.com/en-gb/nuget/schema/… Oct 3, 2017 at 13:50
  • Doesn't explain why my extra DLLs in the lib folder aren't being added as references automatically without the <references> element though... Oct 3, 2017 at 13:51
  • Ok, apparently support for DLLs in the root of the lib folder has been deprecated. learn.microsoft.com/en-gb/nuget/create-packages/… Oct 3, 2017 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.