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,
Add a new class ConsoleOutput with below code, which will print Caller method name and path of it in Console,
- using System;
- using System.Runtime.CompilerServices;
-
- namespace NugetDemo {
- public class ConsoleOutput
- {
- public void LogConsole([CallerMemberName]string methodName="", [CallerFilePath]string path="") {
- Console.WriteLine($"{methodName} : {path}");
- }
- }
- }
Let’s create a Nuget package for the library by going to Project properties and clicking on Package tab,
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.
We can click on Pack action under project context menu as a shortcut to create the package,
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,
By using NuGet Package Explorer, we can examine the contents of the package,
We can even verify the contents by changing .nupkg extension to .zip,
I am ending things here. I hope this is informative.