0

I'm building a .net core project using AppVeyor and publish a nuget package as result. I have a content file specified:

  <ItemGroup>
    <Content Include="ExchangeScripts\getMarket.js">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <CopyToPublishDirectory>Always</CopyToPublishDirectory>
    </Content>
  </ItemGroup>

I expected it to get into a package but when I eventually reference a nuget package (from AppVeyor's internal storage) in an app, I get "file not found exception".

Are there any extra steps to make it happen that I'm missing?

SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
  • What king of project is it? I believe that [this](https://stackoverflow.com/a/40557532/6733637) is still true... Does it works for you on local machine? – Ilya Finkelsheyn Jun 09 '18 at 00:30
  • @ilyaf, it's a .net core console project – SiberianGuy Jul 08 '18 at 09:12
  • What path are you looking at? It would likely be at `OUTPUTDIR\ExchangeScripts\getMarket.js` not `OUTPUTDIR\getMarket.js` Also with core it can be `Release\netcoreapp2.0\ExchangeScripts\getMarket.js` – Thymine Jul 09 '18 at 18:18
  • Can you get the nuget file downloaded and then open it with Nuget Package Explorer? And/or install it locally and see if that file is included and what the exact path to it is? There are a number of steps where it can break down with how much information you included in this question – Thymine Jul 09 '18 at 18:24

1 Answers1

0

Confirm that package contains content file(s)

If you are uncertain about whether a content file is being included in a nuget package, then you can quickly confirm this by opening the .nupkg package file and attempting to locate the content files that you are trying to include. According to the documentation:

Technically speaking, a NuGet package is just a ZIP file that's been renamed with the .nupkg extension and whose contents match certain conventions.

Since you have set the build action for your file to Content (as you indicated above), you should expect to find it there, likely in both the content and contentFiles directories. (For future reference, you may find it useful to acquaint yourself a bit with the internal structure/conventions of nuget packages.)

Once you have confirmed that your content file is contained in your package, then it seems you only need to figure out how to access the contents of that file.

Option 1: Access content file from a Nuget package from within another project

If you want to be able to reference the content file which is contained in the Nuget package from another project, then first add the Nuget package reference to the new project. Once you do this, you should see the content file(s) included by the package show up in the referencing project. At this point, you can now set the Build Action and Copy to Output Directory options as you normally would, just as though you had added those files directly to the referencing project.

Here is an example of this being done using Visual Studio 2017:

console project and class library project

In the example above, TestLib is a .net core 2.0 class library which only contains a directory with 2 files. Each has a Build Action of Content and a Copy to Output Directory value that corresponds to its name. A nuget package was created from TestLib and subsequently referenced by the ConsoleApp project. The Content directory from the TestLib project (with all of its files) shows up in the ConsoleApp project automatically once the package reference is added. However, by default the Copy to Output Directory option will be set to "Do not copy" for all content files, so you will likely wish to change this, based on your project file snippet above.

If, however, your requirements don't allow you to change the Copy to Output Directory option on the content file from the referencing project, and you wish for it to be set by default to Always instead of Never, then I'll refer you to this SO answer for how to accomplish that. (Hint: If you are not already using a .nuspec file, then you can copy the auto-generated one from within the .nupkg file to use as a starting point.)

[Unlikely] Option 2: Access content file from a Nuget package directly

If you wanted to access the content file from the Nuget package directly, then you could automate the process of extracting the content file(s) from the .nupkg file and doing with it according to your needs. I don't think this is what you wanted, but I imagine that someone has wanted to do this before.

Even if this hasn't given you the answer you were seeking, I hope this has helped in some way.

porcus
  • 823
  • 8
  • 11