Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a Nuget package with Visual Studio 2017

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?

like image 946
Nikki Locke Avatar asked Jun 20 '17 15:06

Nikki Locke


People also ask

How do I create a NuGet package?

The typical way of getting a NuGet package is installing it to a project using an IDE like JetBrains Rider, package manager, or other tools. To help you install a package, Packages generate code snippets for popular tools: dotnet , NuGet package manager, and other. In Packages, find the desired NuGet package.

How do I open NuGet package manager in Visual Studio?

Open your project or solution in Visual Studio, and select Tools > NuGet Package Manager > Package Manager Console to open the Package Manager Console window.


1 Answers

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).

like image 130
Martin Ullrich Avatar answered Oct 15 '22 11:10

Martin Ullrich