CodeDigest.Com Logo
Featured:

Learn Nuget Package Manager in 10 Minutes

Nuget is a package manager to assist in developing applications in Microsoft development platforms including .Net. Nuget package manager supports both GUI based and PowerShell command based tools to search and install packages into our project.


 

 

Nuget package manager relieves developers from doing the redundant tasks of downloading and configuring a re-usable component or package (both open source and Microsoft delivered) into the project. Nuget takes care of this task now, it will help developers to find the package and get it downloaded and configured in the project automatically.  All package information are stored in a centralised repository which Nuget tools (GUI and PowerShell based) uses to download and configure the package into our projects. The component (or package) developer will create a Nuget package and register it with Nuget centralised repository for developers from all over the world to consume through Nuget.

Nuget package manager is now part of Visual Studio installations as Visual Studio extension. Until Visual Studio 2015, the Nuget package information are maintained in a XML based file called packages.config and the package binaries are downloaded into a folder called Packages (under project folder where you find the solution file). In Visual Studio 2015, these were moved into a new file called project.json file. With the release of Visual Studio 2017, the project.json is retired and the Nuget packages are maintained as part of the Visual Studio project file itself (.csproj). The Nuget package that our project uses are commonly referred as dependencies.

For Asp.Net Core projects (the newer version of Asp.Net), Nuget package manager plays a very important role. Asp.Net Core framework is completely open source and modular in nature. All framework components including .Net runtime are packaged and available as Nuget packages. This means, for any Asp.Net Core projects all the projects dependent packages and runtime components are included in the project by the Nuget package manager.

A sample Visual Studio 2017 .csproj file with package information.

 

<ItemGroup>

    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />

    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />

    <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.1" />

    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.1" />

    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" />

    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />

    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />

    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" PrivateAssets="All" />

    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />

    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.1" PrivateAssets="All" />

    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" PrivateAssets="All" />

    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.1" />

    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />

    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0" PrivateAssets="All" />

    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />

  </ItemGroup>

 

Using Nuget Package Manager

To add a new Nuget package into a project, right click project in solution explorer and click “Manage Nuget Packages..”

This will bring Nuget GUI like below,

The Browse tab helps you to search an available package and install it. For example, type “Entity Framework” in Search textbox and enter. This will load the Entity Framework Nuget package in results section. Click “Install” to install the package into the project. It will ask user consent for End User Agreement during installing, click “Agree”.

The Installed tab lists all the packages we have included in our project.

The Updates tab list only packages that has a latest update available.

Using Nuget Package Manager Tools from PowerShell

Click Tools> Nuget Package Manager > Package Manager Console. Refer below image.

This will load the package manager console window at the bottom in Visual Studio.

Syntax to install a package,

 

Install-Package [packagename]

 

Ex: To install Entity Framework package,

 

install-package entityframework

 

 

Nuget PowerShell Commands

Command

Description

Find-Package

Get the set of packages available from the package source, based on the package Id/keyword. This is a new command that will replace Get-Package -ListAvailable.

Get-Package

Gets the set of installed packages. With -Updates switch, gets the set of package updates available from the package source.

Install-Package

Installs a package and its dependencies into the project.

Uninstall-Package

Uninstalls a package. If other packages depend on this package, the command will fail unless the –Force option is specified.

Update-Package

Updates a package and its dependencies to a newer version.

Sync-Package

Get the version of installed package from specified/default project and sync the version to the rest of projects in the solution.

Add-BindingRedirect

Examines all assemblies within the output path for a project and adds binding redirects to the application (or web) configuration file where necessary.

Get-Project

Returns a reference to the DTE (Development Tools Environment) for the specified project. If none is specifed, returns the default project selected in the Package Manager Console.

Open-PackagePage

Open the browser pointing to ProjectUrl, LicenseUrl or ReportAbuseUrl of the specified package.

Register-TabExpansion

 

Registers a tab expansion for the parameters of a command.