Tuesday, June 8, 2021

NuGet Package


Meaning of Package

Package is compiled code plus some other contents which are useful in a project. These useful packages are created, shared and consumed in different projects.

What is NuGet Package?

A NuGet package is a single ZIP file with the .nupkg extension that contains compiled code (DLLs), other files related to that code, and a descriptive manifest that includes information like the package's version number. 

Developers create packages and publish them to a public or private host. Package consumers obtain those packages from suitable hosts such as NuGet, add them to their projects, and then call a package's functionality in their project code.

NuGet as Host between Producer and Consumer
NuGet works as mediator or host between package creator and consumer to share the packages. NuGet is an essential tool for any modern development platform through which developers can create, share, and consume useful code. As a host NuGet serves as the point of connection between package creators and package consumers.

NuGet defines how packages for .NET are created, hosted, and consumed, and it provides the tools for each of those roles.

Public and Private Packages

Because NuGet supports private hosts alongside the public nuget.org host, we can use NuGet packages to share code that's exclusive to an organization or a work group.

Massive Number of Packages
In its role as a public host, NuGet itself maintains the central repository of over 100,000 unique packages at nuget.org. These packages are employed by millions of .NET/.NET Core developers every day. 

Private Packages
NuGet also enables us to host packages privately in the cloud (such as on Azure DevOps), on a private network, or even on just the local file system. By doing so, those packages are available to only those developers that have access to the host, giving users the ability to make packages available to a specific group of consumers.

NuGet Tools & Commands, Basic concepts

The dotnet CLI tool allows developers to easily install, uninstall, and update NuGet packages in projects and solutions. It runs on Windows, Mac OS X, and Linux. The dotnet CLI can be used in .NET Core and .NET Standard project (SDK-style project types), and for any other SDK-style projects 

Command to install a Nuget package:
Syntax:
dotnet add package <PACKAGE_NAME>
Example:
dotnet add package Newtonsoft.Json
Note: After the command completes, please look at the project file to make sure the package was installed. We can open the .csproj file to see the added reference:
<ItemGroup>
  <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>

Command to Install a specific version of a package
If the version is not specified, NuGet installs the latest version of the package. You can also use the dotnet add package command to install a specific version of a Nuget package:
Syntax:
dotnet add package <PACKAGE_NAME> --version <VERSION>
Example:
dotnet add package Newtonsoft.Json --version 12.0.1

Command to List package references
We can list the package references for our project using the dotnet list package command.
Syntax:
dotnet list package

Command to remove a package
We can use the dotnet remove package command to remove a package reference from the project file.
Syntax:
dotnet remove package <PACKAGE_NAME>
dotnet remove package Newtonsoft.Json

Characteristics of NuGet

  1. NuGet provides the central nuget.org repository with support for private hosting.
  2. NuGet provides the tools developers need for creating, publishing, and consuming packages.
  3. Most importantly, NuGet maintains a reference list of packages used in a project and the ability to restore and update those packages from that list.

No comments:

Post a Comment

Hot Topics