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!
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.
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.
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).
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.
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:
Errors
class was accessible, although it wasn't visible anywhere in the project.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With