Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NuGet add external reference

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?

like image 812
Carl Bussema Avatar asked Jan 06 '12 20:01

Carl Bussema


2 Answers

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.

like image 174
Matt Ward Avatar answered Oct 17 '22 02:10

Matt Ward


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

like image 28
Mustafakidd Avatar answered Oct 17 '22 00:10

Mustafakidd