- .NET Core - Migrations
- Restoring and Building & MSBuild
- .NET Core - MSBuild & project.json
- .NET Core - SDK
- Managed Extensibility Framework
- .NET Core - Testing Library
- Running Tests in Visual Studio
- .NET Core - Create a Testing Project
- .NET Core - PCL Troubleshooting
- Creating a Xamarin.Forms Project
- Sharing .NET Core Libraries
- Adding References to Library
- .NET Core - Portable Class Library
- Create .NET Standard Library
- Windows Runtime & Extension SDKs
- .NET Core - Metapackage
- .NET Core - MSBuild
- Create UWP App with .NET Core
- .NET Core - Package References
- .NET Core - Project Files
- .NET Core - Modularity
- .NET Core - Code Execution
- .NET Core - Garbage Collection
- .NET Core - Numerics
- .NET Core - Getting Started
- .NET Core - Environment Setup
- .NET Core - Prerequisites
- .NET Core - Overview
- .NET Core - Home
.NET Core Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
.NET Core - Sharing Libraries
In this chapter, we will discuss how to share your pbrary as NuGet Package so 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 pubpc 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 pbrary. To do so, open the command prompt and go to the folder where the project.json file of your pbrary project is located.
Let us now run the following command.
dotnet help
At the end, you can see different commands pke 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
You can now see that the NuGet packages are produced in the bin folder; let us open the binDebug 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.
Select the first option Open a local package.
Select the StringLibrary.1.0.0.nupkg and cpck Open.
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 pbrary pke Id, Versions and all the of the dependencies.
Let us now open the StringLibrary.1.0.0.symbols.nupkg.
In this NuGet package, you will see the source files and the *.pdb file as well. If you double-cpck on the StringLib.cs file, you see the source code as well.
Here the question is, how can configure the metadata pke 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.Compatibipty": "1.0.1", "NETStandard.Library": "1.6.0", "System.Runtime.Seriapzation.Json": "4.0.3", "System.Runtime.Seriapzation.Primitives": "4.3.0" }, "frameworks": { "netstandard1.3": {} } }
You can now see additional information pke author name, description and version added here. Let us save this file, build the pbrary project, then execute the “dotnet pack” command again.
Inside the binDebug folder, you can see that the StringLibrary NuGet packages are produced with version 1.0.1; let us open it in NuGet Package Explorer.
You will see the updated metadata. The question now is, how can we use it in another package.
We need to start by pubpshing somewhere in the NuGet feed and then we can consume it in another project.
There are two options to pubpsh the updated metadata −
Pubpsh 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 pubpsh your package to nuget.org, you can follow all the guidepnes specified here
.Follow these steps to push the updated metadata to private NuGet feed.
Step 1 − To start with, we need the nuget commandpne utipty and we have to install it. Let us now open the NuGet Package Manager and search for nuget.commandpne.
Step 2 − Select Nuget.Commandpne and cpck Install.
Step 3 − Cpck OK to install Nuget.Commandpne. You can also manually install it by downloading it from the following Url
and then set up the environment variable.Step 4 − Once the installation is finished, let us open the command prompt again and go to the binDebug 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 StringLibrary can further be added to the private feed.
Step 7 − Let us go to that folder.
Step 8 − Inside the stringpbrary folder, you will see another folder with the version name and here it is 1.0.1.
The NuGet package is located here.
Advertisements