Nuget – Walk-through – (Part 1)

More Info: https://docs.nuget.org/consume

NuGet is the package manager for the Microsoft development platform including .NET. The NuGet client tools provide the ability to produce and consume packages.

When you install a package, NuGet copies files to your solution and automatically makes whatever changes are needed, such as adding references and changing your app.config or web.config file. If you decide to remove the library, NuGet removes files and reverses whatever changes it made in your project so that no clutter is left. Everything necessary to install a library or tool is bundled into a package (a .nupkg file).

get-package -listavailable -filter elmah

get-help Find-Package -examples

get-package -updates

To check if there are newer versions available for any installed packages, enter Get-Package -updates at the prompt.

install-package

install-package

install-package

uninstall-package

update-package

we can use update-package command for both upgrading and downgrading.

if you remove reference manually from References folder (right click and delete) it will not delete reference from packages.config file. If you want to restore the package you can use update-package – reinstall command.

Host Your Own NuGet Server or How To Create Local NuGet Server

NuGet requires Visual Studio. The NuGet Package Manager Console requires that PowerShell 2.0 be installed.

$host

Extend NuGet Command Line

$dte package manager console

A solution-level package has to be installed only once in a solution to be available for all projects in the solution. A project-level package must be installed separately in each project where you want to use it.

How to access NuGet when NuGet.org is down (or you’re on a plane)

You can turn off source control integration for the Packages folder by setting the “disableSourceControlIntegration” property in the nuget.config to “true”.

This key works at the solution level and hence need to be added to the NuGet.config file present in the “$(SolutionDir).nuget directory“. Enabling package restore from VS would add the .nuget\nuget.config file automatically.

NuGet will identify the packages.config and project.json files in the same folder as the project file.

The naming convention is as follows:

  • packages.config shall match the pattern packages.{project-name}.config
  • project.json shall match the pattern {project-name}.project.json
%appdata%\nuget

For test purposes, you can use staging.nuget.org, or alternative public nuget servers like https://myget.org (public and private feeds) or Visual Studio Online .

nuget.config for projects, To inherit the global NuGet package sources remove the <clear/> line

Bulk Publishing NuGet Packages

In projects using NuGet we are going to have two types of configuration files.

Project.json is the file replacing packages.config as the file specifying the packages used by the project.

  1. Packages.config
  2. Project.json – Added in NuGet 3.0

Packages.config: The format utilized up until NuGet 3 with any project consuming NuGet packages. It is an XML file with a flat list of packages. Requires to change the .csproj file, and sometimes the app.config/web.config file as well as other files within the project.

Project.json: installing a package simply means adding the package id and version to the project.json file. There is no dependency resolution at this point nor a change to the project file.

project.json is mandatory for UWP (Universal Windows Platform) apps and ASP.NET 5 apps, is optional for PCLs, and currently is not fully supported on other project systems.

nuget 3.0: Install/Uninstall scripts These scripts are not supported and will be ignored. In case they exist in the package a project using transitive restore.
It is not necessary to check in the lock file. A lock file will be auto generated whenever a restore happens, so you can safely leave it out of source control and avoid accidental merge conflicts.

 

the .nupkg file, which is a .zip file that contains all of the files included in the package.

NuGet can display packages from multiple package sources.

get-help get-package

Some packages install new commands that you can use in the Package Manager Console window. One example of such a package is MvcScaffolding, which creates commands you can use to generate ASP.NET MVC controllers and views. The following illustration shows that installing MvcScaffolding creates a new command Scaffold, complete with tab expansion.

$profile

This file doesn’t necessarily exist by default. You can run the following set of commands to create it.

 

Viewing a DGML diagram is only supported in Visual Studio Premium or Higher.

NuGet Gallery, the NuGet commandline, the Package Manager Console and Dialogs all share the same search formats.

Use the .gitignore file to ignore the packages folder.

Given a folder D:\projects\contoso that contains a single solution file contoso.sln, the following variations of the command will each restore all packages used by projects within the solution:

default installation folder of nuget:

C:\Users\{yourUserName}\AppData\Local\NuGet

nuget configuration file

The default location for NuGet’s configuration file is %APPDATA%\NuGet\NuGet.Config (DOS) or $ENV:APPDATA\NuGet\NuGet.Config (PowerShell).

Below path locations {IDE} can be VisualStudio and if you want to specify config for a particular SKU of Visual Studio {SKU} can be Pro, VWDExpress, VPDExpress, VSWinExpress or VSWinDesktopExpress.

Credentials for package source

Allows you to set the credentials to access the given package source This key has to be set using the NuGet.exe Sources command. The default behavior is to store the password encrypted in the config file

%ProgramData%\NuGet\NuGetDefaults.config.

While not required, it is expected many companies to use Group Policy to deploy NuGetDefaults.config files to developers’ machines.

 

nuget update

nuget update -Safe
Looks for updates with the highest version available within the same major and minor version as the installed package.

nuget update -Self
(v1.4) Update the running NuGet.exe to the newest version available from the server.

Restore: Downloading and unzipping (restoring) any packages missing from the packages folder.

Init Command

Specify the path to source package source to be copied from and the path to the destination package source to be copied to.

examples:

Add Command Adds the provided package to your package source in a hierarchical layout.

Locals Command Clears or lists local NuGet resources such as http request cache, packages cache, or machine-wide global packages folder.

Help Command
Displays general help information and help information about other commands.

Credential Providers can also be stored anywhere and then made accessible to NuGet.exe via an environment variable. To use a Credential Provider this way, set the %NUGET_CREDENTIALPROVIDERS_PATH% to the location of your Provider.

To make a Credential Provider available to all instances of NuGet.exe run under the current user’s profile, add it to %LocalAppData%\NuGet\CredentialProviders. You may need to create the CredentialProviders folder.

Reinstalling a package is a simple act of uninstalling a package and installing back the same version of the package that was uninstalled.

Since the version of the package dependency that worked best is determined by you, use one of the following:

  • Update-Package –reinstall <packageName> when updating the package dependencies is required for fixing an issue
  • Update-Package –reinstall <packageName> -ignoreDependencies when updating package dependencies might result in a broken state.

Leave a Reply

Your email address will not be published. Required fields are marked *