Look At NuGet Packages In VS 2017

In the article, we will walk through creating a nuget package targeting .NET Standard in VS 2017 and NuGet 4.0. .NET Standard is a formal specification of .NET APIs intended to be available on all .NET runtimes like Core, UWP etc. and defines a uniform set of Base Class Library APIs for all .NET platforms to implement, independent of workload. It also enables developers to produce portable libraries that are usable across .NET runtimes.

In previous versions of Nuget, we need to create a separate NuProj with necessary metadata added in .nuspec file. Starting with NuGet 4.0 and .NET Core\Standard projects, we can add package metadata within the .csproj itself.

Let’s create a sample NuGet project on .NET Standard in VS 2017 to understand it better. Create a class library targeting .NET Standard as shown below,

VS 2017

Add a new class ConsoleOutput with below code, which will print Caller method name and path of it in Console, 

  1. using System;  
  2. using System.Runtime.CompilerServices;  
  3.   
  4. namespace NugetDemo {  
  5.     public class ConsoleOutput  
  6.     {  
  7.         public void LogConsole([CallerMemberName]string methodName="", [CallerFilePath]string path="") {  
  8.             Console.WriteLine($"{methodName} : {path}");  
  9.         }  
  10.     }  
  11. }   

Let’s create a Nuget package for the library by going to Project properties and clicking on Package tab,

VS 2017

Click on “Generate Nuget Package on build” and enter necessary details about the package. When we build the project, NuGet package will be generated in Debug folder and same can be published on NuGet or jFrog for distribution.

VS 2017

We can click on Pack action under project context menu as a shortcut to create the package,

VS 2017

As well, we can use MSBuild tool to create package by running below command from command line in the same folder as the .csproj file,

VS 2017

By using NuGet Package Explorer, we can examine the contents of the package,

VS 2017

We can even verify the contents by changing .nupkg extension to .zip,

VS 2017

I am ending things here. I hope this is informative.


Similar Articles