NuGet is the package manager for the Microsoft development platform including .NET. It enables developers to create, share, and consume reusable code packages. These packages are distributed from the public NuGet Gallery (nuget.org) or private repositories. NuGet helps manage dependencies, versioning, and package configurations in both .NET Framework and .NET Core/5/6+ projects.
The easiest way to install NuGet packages in Visual Studio is by using the Package Manager GUI.
Install-Package Newtonsoft.Json
This command installs the specified package and its dependencies into your project.
For .NET Core/.NET 5+ applications, use the CLI for cross-platform compatibility.
dotnet add package Newtonsoft.Json
To see all installed packages:
dotnet list package
After installation, the NuGet package is referenced in the project file (.csproj).
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
This is different from older projects that used a separate packages.config file.
using Newtonsoft.Json;
using System;
public class Demo
{
public static void Main()
{
var person = new { Name = "John", Age = 30 };
string json = JsonConvert.SerializeObject(person);
Console.WriteLine(json);
}
}
dotnet new classlib -n MyAwesomeLibrary
cd MyAwesomeLibrary
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<PackageId>MyAwesomeLibrary</PackageId>
<Version>1.0.0</Version>
<Authors>YourName</Authors>
<Company>YourCompany</Company>
<Description>A demo NuGet package</Description>
</PropertyGroup>
</Project>
dotnet pack
This generates a .nupkg file in the bin/Debug or bin/Release directory.
Visit nuget.org, log in, and generate an API Key under your account settings.
dotnet nuget push bin/Debug/MyAwesomeLibrary.1.0.0.nupkg --api-key YOUR_API_KEY --source https://api.nuget.org/v3/index.json
If you want to share packages within an organization without publishing to nuget.org:
mkdir C:\NuGetPackages
dotnet nuget add source C:\NuGetPackages -n LocalFeed
dotnet add package MyAwesomeLibrary --source C:\NuGetPackages
Stores configuration settings such as package sources and credentials.
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="LocalFeed" value="C:\NuGetPackages" />
</packageSources>
</configuration>
dotnet list package --outdated
dotnet add package [PackageName] --version x.y.z
To restore packages from a solution or project file:
dotnet restore
Make sure the package name and version are correct. Verify that your nuget.config file contains the proper source.
Use dotnet list package and examine the dependency tree to resolve conflicts manually.
dotnet pack --include-symbols --include-source
Azure DevOps provides a private NuGet feed via Artifacts. This is ideal for internal teams.
GitHub Packages supports NuGet hosting. You'll need a token and modify nuget.config with GitHub credentials.
NuGet is an essential tool in the .NET developer's toolbox. It enables scalable and maintainable project architecture by simplifying dependency management. Whether you're consuming existing packages or authoring your own, mastering NuGet will significantly boost productivity and collaboration. With support across Visual Studio, CLI, and CI/CD tools, NuGet is a cornerstone of modern .NET development.
Copyrights © 2024 letsupdateskills All rights reserved