8

I am building a .net 4.5 class library dll using Visual Studio 2017.

I wish to package up this dll, and some related folders of javascript, html, css and suchlike files as a NuGet package.

Unfortunately, I don't know where to start - the only instructions I can find for creating NuGet packages apply to Visual Studio 2015. I am told NuGet is included in Visual Studio 2017, but if I open a Developer Command Prompt for VS 2017, and type Nuget, the command is not found.

I get the impression that Visual Studio 2017 can build NuGet packages in .Net standard projects, but not in other types of project.

Am I supposed to go back to Visual Studio 2015, or what?

Nikki Locke
  • 2,759
  • 6
  • 29
  • 53

1 Answers1

10

There's a few distinct questions here:

About the VS integration: In VS < 2017 NuGet was usually an extension that could be updated individually. In VS 2017 the extension is tightly integrated and updated via updates to VS itself. This never included the command-line nuget.exe that is usually used to pack and push packages - this command line client is available from NuGet's download page.

The "classic" approaches to packaging .NET projects still work and are documented in nuget's documentation page, especially important is the section "Creating the .nuspec file … from a Visual Studio project".

VS 2017 also introduces a new type of projects that use the ".NET Sdk" that evolved from the .NET Core tooling. These projects are integrated with NuGet and can be packed by VS and directly from MSBuild / dotnet pack. This project type can also be used to create .NET Framework NuGet packages. However, there isn't a template in VS since some features available for classic .NET projects aren't implemented (e.g. Designers for xaml,edmx). But for most logic libraries you can create a .NET Standard project and edit the csproj file to change

<TargetFramework>netstandard1.6</TargetFramework>

to

<TargetFramework>net461</TargetFramework>

so the project will target .NET 4.6.1 (other versions possible). This project will have the same integrated packing functionality as .NET Standard and .NET Core projects. You can follow the Guide "Create .NET Standard Packages with Visual Studio 2017" but perform that change to the project file after creating it.

In order to include items into the package, you can use the following metadata:

<ItemGroup>
  <Content Include="**\*.txt" Pack="true" />
</ItemGroup>

This will put the files into both a content and contentFiles directory in the resulting nuget. When the resulting package is consumed via a ProjectReference, the contentFile needs an additional metadata attribute to make sure that the referencing project copies it to its output on build:

<ItemGroup>
  <Content Include="**\*.txt" Pack="true" PackageCopyToOutput="true" />
</ItemGroup>

This property however is only supported in the upcoming VS 2017 15.3 update / .NET Core SDK 1.1/2.0 (not yet released at the time of writing).

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • The Nuget download page specifically says NuGet is included in VS2017, so I don't understand why it isn't. The statement certainly put me off downloading the command line versions (which I assumed must be for earlier versions of VS). I did look at the nuget documentation page, and it referred me to https://learn.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package, which starts off by telling me to install VS 2015! You can see how I got confused. – Nikki Locke Jun 20 '17 at 18:05
  • I have got a little way down the route of creating a NET standard project and retargeting it to NET4.5, but as my previous question at https://stackoverflow.com/questions/44498659/how-do-you-add-additional-files-to-a-nuget-package-in-visual-studio-2017/44660029#44660029 states, there is little or no documentation on how to add additional files into the package (and, ideally, mark them as to be copied to the output directory when the referencing project is compiled). – Nikki Locke Jun 20 '17 at 18:07
  • Added a paragraph about the content files. The last step isn't in released parts yet however :(, I had filed the GitHub issue for it https://github.com/NuGet/Home/issues/5259 which was resolved very recently. – Martin Ullrich Jun 20 '17 at 18:16
  • Using it in "classic" projects should copy the files to the consuming project – Martin Ullrich Jun 20 '17 at 18:16
  • May I suggest you update the quick start document to include instructions for VS 2017? – Nikki Locke Jun 21 '17 at 18:55
  • Also, can I ask, is it possible to include content files automatically, or via the VS gui, without editing the csproj? – Nikki Locke Jun 21 '17 at 18:57
  • I don't think the GUI has options for that.. so you'd have to add an `` to the csproj file manually. – Martin Ullrich Jun 21 '17 at 18:58
  • Sorry, another suggestion - you mention in https://learn.microsoft.com/en-us/nuget/guides/create-net-standard-packages-vs2017 editing the csproj to fill in stuff like Authors and Description. Is it not simpler to use the Project Properties Pack gui page? – Nikki Locke Jun 21 '17 at 18:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147303/discussion-between-nikki-locke-and-martin-ullrich). – Nikki Locke Jun 21 '17 at 19:00
  • Yes the pack property page can do that. There's just no UI for the content stuff. (i'm not affiliated with MS btw and never edited those docs..) – Martin Ullrich Jun 21 '17 at 19:01