NuGet Tools for .NET Core

This section provides comprehensive documentation on using NuGet, the package manager for the .NET ecosystem, with .NET Core.

Introduction to NuGet

NuGet is the package manager for .NET. It enables developers to discover, install, and share reusable code. .NET Core leverages NuGet extensively for managing project dependencies, including libraries, frameworks, and tools.

Key Concepts

  • Packages: Units of code that can be reused across multiple projects.
  • Package Manager Console: An interactive console within Visual Studio for managing NuGet packages.
  • dotnet CLI: A cross-platform command-line interface for .NET development, including NuGet operations.
  • nuget.config: Configuration files to control NuGet behavior.

Using NuGet with the dotnet CLI

The dotnet CLI is the primary way to interact with NuGet for .NET Core projects on any operating system.

Installing a Package

dotnet add package [PACKAGE_NAME] [--version [VERSION]]

Example: Installing the Newtonsoft.Json package version 13.0.1

dotnet add package Newtonsoft.Json --version 13.0.1

Listing Installed Packages

dotnet list [PROJECT_PATH] package

Example: List packages for the current project

dotnet list package

Updating Packages

dotnet add package [PACKAGE_NAME]

Running dotnet add package with an existing package name will update it to the latest stable version by default. To specify a version:

dotnet add package [PACKAGE_NAME] --version [VERSION]

Removing a Package

dotnet remove package [PACKAGE_NAME]

Restoring Packages

This command downloads and installs all packages required by the project.

dotnet restore [[PROJECT_PATH]]

NuGet Configuration

NuGet behavior can be customized using nuget.config files.

  • Global Configuration: Located at %appdata%\NuGet\NuGet.Config on Windows or ~/.nuget/NuGet/NuGet.Config on macOS/Linux.
  • Project-Specific Configuration: A nuget.config file placed in the project directory or its parent directories.

Common Configuration Options

  • Package Sources: Specify where NuGet should look for packages.
  • Package Management Settings: Control dependency resolution and other behaviors.
<configuration> <packageSources> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" /> <add key="my-local-feed" value="C:\local-feed" /> </packageSources> </configuration>

Best Practices

  • Always use the latest stable versions of NuGet packages where possible.
  • Pin package versions in your project files to ensure reproducible builds.
  • Be cautious when installing packages from untrusted sources.
  • Regularly update your packages to benefit from security patches and new features.