Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NuGet 3.3+: contentFiles not added to project

Tags:

nuget

After reading everything I could find about the new contentFiles element of NuGet 3.3+, I still can't manage to make it work in my package. I've got a package that targets both net46 and uap10.0, and the selection of the right DLLs for the project type and platform work as expected. But I would also like to add two files to the project on package installation, one CSV file for all projects and platforms, and one code file for either C# or VB.Net (with buildAction="Compile"). Here's the abridged version of my latest .nuspec file:

<?xml version="1.0" encoding="utf-8" ?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata minClientVersion="3.3.0">
    ...
    <contentFiles>
      <files include="any\any\ErrorCodes.csv" buildAction="None" copyToOutput="false" />
      <files include="cs\any\Errors.cs.pp" buildAction="Compile" />
      <files include="vb\any\Errors.vb" buildAction="Compile" />
    </contentFiles>
  </metadata>
  <files>
    <file src="contentFiles\any\any\ErrorCodes.csv" target="contentFiles\any\any\" />
    <file src="contentFiles\cs\any\Errors.cs.pp" target="contentFiles\cs\any\" />
    <file src="contentFiles\vb\any\Errors.vb" target="contentFiles\vb\any\" />
    ...
  </files>
</package>

The package is created without errors, and it does contain the three files in the contentFiles folder with the specified directory structure.

But when I install the package - I tried it with with both an Universal App (C# and VB) and a .NET 4.6 console app I modified to use a project.json file - the reference to the DLL is added, but the content files are neither added to the project structure, nor copied into the project directory.

I am thankful for any input!

like image 235
Daniel Kopp Avatar asked Mar 14 '16 10:03

Daniel Kopp


People also ask

What is contentfiles in NuGet?

In NuGet 3.3, the contentFiles feature was introduced to support project.json managed projects and packages that are indirectly referenced in a project. This was an important change because it brings the ability to deliver static files, .pp file transforms, and language specific code into a project.

How do I add content files to a NuGet package?

As prerequisite find or create a NuGet package with some content files, e.g.: Create a new .NET Framework class library project. Install this package. Observe that the content files (e.g. scripts\*.ps1) are added to the project.

Are files copied over after installing a NuGet package?

Files are not copied over. After installing the package, it creates a shortcut to the file which resides in the NuGet cache. But the framework I am using expects those files in a specific folder (which should be created by copying the files).

Is there a way to add content files to a project?

The contentFiles option is not available for other project types. A first example that we can look at would be to include a png of your organization’s logo so that all applications have the same logo in the ‘About’ page. In the case of NuGet, we would want to use the same .NET Foundation logo everywhere.


1 Answers

OK, I found that the contentFiles are actually working, but not in the way I expected it to. A short description for everybody who couldn't find his/her contentFiles:

  • NuGet 3.3+ contentFiles are not added to the project structure. You will not see them in Visual Studio.
  • Instead, the files are extracted into a folder in the project's obj folder, and referenced (if the file is code). You will even see the classes or methods in your contentFiles code files available in IntelliSense. In my case, the Errors class was accessible, although it wasn't visible anywhere in the project.
  • The path of the extracted files has the following schema: \obj\<Debug or Release>\NuGet\<some GUID>\<NuGet package name>\<version>\

I'm not sure if I like this new mechanism. Code coming from some invisible source I cannot see in Visual Studio (a library shipped via NuGet is visible in References, any code it brings via contentFiles is not) may pose problems at times. I actually discovered that the contentFiles mechanism is working when I got an ambiguity error in Visual Studio because the "invisible" Errors.cs from the NuGet package collided with the "visible" Errors.cs I had added to the project manually.

like image 105
Daniel Kopp Avatar answered Oct 30 '22 03:10

Daniel Kopp