.NET Core - Sharing Libraries

In this chapter, we will discuss how to share your library as NuGet Packageso that it can be consumed within another project. Creating a package starts with the code you want to package and share with others, either through the public nuget.org gallery or a private gallery within your organization. The package can also include additional files such as a readme that is displayed when the package is installed, and can include transformations to certain project files.

Let us now consider a simple example in which we will create a NuGet package from our library. To do so, open the command prompt and go to the folder where the project.json file of your library project is located.

Example

Let us now run the following command.

dotnet help 

 

Command

At the end, you can see different commands like new, restore and build, etc.

The last command is pack; this will create a NuGet package. Let us now execute the following command.

dotnet pack

 

Execute

You can now see that the NuGet packages are produced in the bin folder; let us open the bin\Debug folder.

Debug Folder

Now the question is what is inside the NuGet packages, to see that we can use NuGet Package Explorer. Let us now open the NuGet Package Explorer.

Open NuGet

Select the first option Open a local package.

First Option

Select the StringLibrary.1.0.0.nupkg and click Open.

Click Ok

You can see that in the Package contents section we have StringLibrary.dll only. In the Package metadata section, you will see a bit of information about this library like Id, Versions and all the of the dependencies.

Let us now open the StringLibrary.1.0.0.symbols.nupkg.

Symbols

In this NuGet package, you will see the source files and the *.pdb file as well. If you double-click on the StringLib.cs file, you see the source code as well.

StringLib.cs

Here the question is, how can configure the metadata like version, authors and description, etc.

The project.json file is used on .NET Core projects to define project metadata, compilation information, and dependencies. Let us now open the project.json file and add the following additional information.

{ 
   "authors": [ "Mark Junior" ], 
   "description": "String Library API", 
   "version" : "1.0.1-*", 
   "supports": {}, 
   
   "dependencies": { 
      "Microsoft.EntityFrameworkCore": "1.1.0", 
      "Microsoft.NETCore.Portable.Compatibility": "1.0.1", 
      "NETStandard.Library": "1.6.0", 
      "System.Runtime.Serialization.Json": "4.0.3", 
      "System.Runtime.Serialization.Primitives": "4.3.0" 
   }, 
   "frameworks": { 
      "netstandard1.3": {} 
   } 
}

You can now see additional information like author name, description and version added here. Let us save this file, build the library project, then execute the “dotnet pack” command again.

.Net Pack

Inside the bin\Debug folder, you can see that the StringLibrary NuGet packages are produced with version 1.0.1; let us open it in NuGet Package Explorer.

Version

You will see the updated metadata. The question now is, how can we use it in another package.

We need to start by publishing somewhere in the NuGet feed and then we can consume it in another project.

There are two options to publish the updated metadata −

  • Publish it to nuget.org
  • Push the metadata to private NuGet feed

Here we will be using the private NuGet feed because it is a lot easier than to setup an account on nuget.org. To learn how to publish your package to nuget.org, you can follow all the guidelines specified here https://docs.microsoft.com/en-us/nuget/create-packages/publish-a-package.

Follow these steps to push the updated metadata to private NuGet feed.

Step 1 − To start with, we need the nuget commandline utility and we have to install it. Let us now open the NuGet Package Manager and search for nuget.commandline.

Step 2 − Select Nuget.Commandline and click Install.

Commandline

Step 3 − Click OK to install Nuget.Commandline. You can also manually install it by downloading it from the following Url https://dist.nuget.org/index.html and then set up the environment variable.

Manual Install

Step 4 − Once the installation is finished, let us open the command prompt again and go to the bin\Debug folder where the NuGet packages are located and specify the following command −

nuget add StringLibrary.1.0.1.nupkg -Source D:\PrivateNugetPackages 

Step 5 − In the above command, we add the StringLibrary.1.0.1.nupkg package to our private feed and the location is D:\PrivateNugetPackages, -Source specifies the package source.

Step 6 − You can see that the StringLibrary is installed; the StringLibrarycan further be added to the private feed.

Private Feed

Step 7 − Let us go to that folder.

Folder

Step 8 − Inside the stringlibrary folder, you will see another folder with the version name and here it is 1.0.1.

Version Name

The NuGet package is located here.