0

I've been recently trying to create a .nuspec file that attaches a .dll file as an Embedded Resource. To do so, I've used the contentFiles tag on metadata, setting the buildAction="EmbeddedResource", as described in the section Example contentFiles section on the official documentation.

Below you can see my .nuspec file content:

<package>
    <metadata>
        <id>MyPackage</id>
        <version>1.0.0.0</version>
        <title>MyPackage</title>
        <authors>Matias G Henschel</authors>
        <owners>Matias G Henschel</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>My package description</description>
        <copyright>2017</copyright>
        <contentFiles>
            <files include="myDllFile.dll" buildAction="EmbeddedResource" />
        </contentFiles>
    </metadata>
    <files>
        <file src="content\myDllFile.dll" target="contentFiles" />
    </files>
</package>

This package correctly copies the file inside the target project, but it doesn't apply the Build Action to it, which is crucial for me.

I've also tried using a .targets file, with no success.

If you want to see more, I've also created an Issue on the GitHub page.

PS: IMHO, both documentation on contentFiles and .targets files require some rework, they aren't clear enough and .targets' lacks examples.

Matias G H
  • 41
  • 6

1 Answers1

2

You also need a file entry to actually copy the file to the correct contentFiles folder:

<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    …
    <contentFiles>
      <files include="any/any/MyEmbeddedFile.txt" buildAction="EmbeddedResource" />
    </contentFiles>
  </metadata>
  <files>
    <file src="path/to/MyEmbeddedFile.txt" target="contentFiles/any/any/MyEmbeddedFile.txt" />
  </files>
</package>

Note that this will only work in NuGet 3.3+ with project.json based projects and NuGet 4+ for PackageReference based projects. For projects using this package via packages.config, you will still need to add the file to the content folder and add a custom target to make it the right item type.

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • The file is as recommended and it doesn't work. The only detail is the *any/any* folder structure, which I tried with no success. For reference: I'm using Nuget 4.1. – Matias G H Oct 09 '17 at 19:39
  • just to be clear, which kind of project are you installing this into? the embedded resource won't show up in the solution explorer, you'll need to open the exe/dll in dotPeek / ILSpy or sth. also note that you need to clear your NuGet cache unless you always increase the version number on every try using `dotnet nuget locals all --clear` – Martin Ullrich Oct 10 '17 at 03:39
  • I'm trying to add it to a Class Library. The version is not a problem, I'm setting it manually and I'm able to install the package correctly. It just doesn't add it the correct way (Embedded Resource). I did use ILSpy to check if the file was in there but no resource was attached. When I manually set it to Embedded Resource, the file showed up in ILSpy. – Matias G H Oct 10 '17 at 11:45
  • Is the consuming project using `ProjectReference` or `packages.config`? because contentFiles doesn't work with `packages.config` – Martin Ullrich Oct 10 '17 at 12:10
  • It uses `packages.config`... But how would I work around it? It should work just like any other NuGet package... – Matias G H Oct 10 '17 at 14:28
  • It doesn't. `contentFiles` is only for `ProjectReference` or `project.json`. – Martin Ullrich Oct 10 '17 at 14:32
  • If I can't use `contentFiles`, what should I use? A `.targets` file? (couldn't figure it out how to) I only need a `.dll` file as an Embedded Resource. That's all. – Matias G H Oct 10 '17 at 16:25