This article was first published on - "What is NuGet?" Why is NuGet in the .NET project? How to use NuGet package? 》
Overview
Hello everyone, I am 1614fd4314f0af Rector who focuses on .NET development.
In the development of .NET application programming, developers usually use class libraries to manage and maintain program codes belonging to the same category to facilitate code reuse. In general, all classes in the same class library are in the same assembly.
After these class libraries are compiled by the compiler, a dynamic link library file with an .dll
.dll
assemblies by reference in other projects and use the encapsulated classes and members.
However, as projects become larger and more numerous, manually referencing the .dll
dynamic link library makes package management and maintenance very difficult. In this case, NuGet package management tool came into being.
NuGet npm package in the front-end development, Maven or Gradle packages in the development of Java rely on their own language tools. Responsible for controlling the package version and maintaining the dependencies between packages. With package management tools, you can quickly install packages, restore packages, etc.
Before officially contacting the NuGet package tool, let’s review the evolutionary history introduced by the package in the .NET project step by step.
Ways to introduce assemblies
There are many ways to introduce assemblies, such as:
- The assembly is in the same solution, and the project is directly referenced
- The assembly is in the local disk, browse the
.dll
file to import - NuGet package management tool installation
1. Reference project method
If a currently named NugetDemo.Payment library and a project called ConsoleApp console application, now you need ConsoleApp incorporated by reference in the item's NugetDemo.Payment , as follows :
1. Right-click dependency , click add project reference , as shown in the figure:
2. In the pop-up Reference Manager window, select NugetDemo.Payment , click confirm , as shown in the figure:
3. Visual Studio will add the selected project to the current project's dependency -> project list, as shown in the figure:
2. Browse file mode
Still in the ConsoleApp project, if there is another Alipay assembly, as shown in the figure:
1. Still refer to the project by reference, right-click dependency , click add project reference
2. In the pop-up Reference Manager window, select browse , as shown in the figure:
3. Then click the browse Ali.Alipay.dll file you just prepared in the file explorer, as shown in the figure:
4. After selecting, click the add button in the lower right corner, and it will automatically return to the following interface:
5. Select the Ali.Alipay.dll that you just browsed, and finally click the button in the lower right corner to confirm the button, Visual Studio will add this .dll file to the dependency -> list of the current project's assembly 580 , As shown in the figure:
It is understandable that the above two methods of referencing the assembly are not problematic in the result. But this method is only suitable for personal projects. When your team members or team projects reach a certain level, problems such as assembly management, maintenance, and version control will give you headaches.
In the end, you may not know which version of the assembly is currently being referenced? Where can I find the correct assembly version? What is the dependency relationship between them? If the assembly is shared?
, these problems can be solved.
NuGet package
The NuGet package is an assembly shared package provided by Microsoft for the .NET (including .NET Core) platform.
Simply put, the NuGet package is a ZIP file with the extension .nupkg, which contains the compiled code (.dll) and other files related to the code, as well as the description of the package version number and other information.
Developers can create code-sharing packages and publish them to public or private hosts. Package users obtain these packages from suitable hosts, add them to their projects, and then call the package functions in their project code. Then NuGet handles all intermediate details (including installation, uninstallation, dependency maintenance, version control, etc.) by itself.
Microsoft officially provides special public hosting services for public .NET shared packages, the address is: https://www.nuget.org/
At present, more than 250,000 packages are shared here, as shown in the figure:
In addition to supporting public nuget.org hosts, NuGet also supports private hosts, so you can build personal or company internal NuGet private servers to achieve the purpose of internal sharing of packages.
Installation and uninstallation of NuGet package
There are many ways to manage NuGet packages, the most commonly used are: one, through the NuGet package manager; two, through the command line management. The NuGet package manager is a client management tool available only in integrated development environments such as Visual Studio or Rider.
Let's take the Visual Studio 2022 preview version (17.0.0 Preview 3.1) as an example to demonstrate.
NuGet package manager
Install NuGet
If there is currently a console application based on .NET 5, the structure is as follows:
Now you need to install Newtonsoft.Json in this console for json serialization and deserialization operations. Then, we can open the NuGet package manager by right-clicking the dependency -> manage the NuGet package
Then select browse the tab, and type keywords in the search box, select the package to be installed in the search results, and finally click the on the right to install the button, as follows:
In the pop-up dialog box, click the confirm the button:
Visual Studio will automatically download the selected package and its dependencies, and add them to the dependencies of the current project, as shown below:
Now, you can call Newtonsoft.Json ConsoleApp1 project. The following example demonstrates the use of Newtonsoft.Json to serialize an entity object string as follows:
using Newtonsoft.Json;
class Program
{
static void Main(string[] args)
{
// 模拟一个JSON字符串
var json = "{\"id\":1,\"name\":\"Rector\",\"age\":18}";
// 调用JsonConvert.DeserializeObject<T>()泛型方法反序列化
var person = JsonConvert.DeserializeObject<Person>(json);
Console.WriteLine(person.ToString());
Console.ReadKey();
}
}
/// <summary>
/// 定义一个与JSON字符串字段匹配的实体类
/// </summary>
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public short Age { get; set; }
/// <summary>
/// 重写ToString()方法
/// </summary>
/// <returns></returns>
public override string ToString()
{
return $"id:{Id},name:{Name},age:{Age}";
}
}
The results of the operation are as follows:
NuGet package version management
NuGet packages are divided into versions. Different versions are identified by the version number, such as the Newtonsoft.Json program package installed above, and the current latest version (13.0.1) is selected during installation, as shown in the figure:
The drop-down lists Newtonsoft.Json packages available version, you can install a different version by selecting different version numbers Newtonsoft.Json package can also be achieved NuGet package versions by this version of the drop-down list The upgraded/downgraded to .
For example, currently we have installed the version 13.0.1
, then choose any one with a smaller version number that is downgrade to , for example, choose to downgrade to 12.0.3
here, click the update button to complete the downgrade.
In the same way, select any version that is larger than the current version number to complete the upgrading .
Q: Why do NuGet packages have different version numbers?
Answer: The version number of NuGet serves as the identification of different versions. The function of a NuGet package is constantly being improved and expanded. Each time the NuGet package is iterated (either to fix bugs or to add new features), a different version number will be assigned to it (usually this version number is cumulative upwards) ), the different versions are independent of each other and do not affect each other.
NuGet package uninstall
There is mounting uninstall, if an item in a NuGet package is no longer needed, you can NuGet package manager unloading button to uninstall a key specified NuGet package (which will depend on the package Are uninstalled together), as shown in the figure:
After uninstalling the Newtonsoft.Json package, the deserialization sample code demonstrated above is compiling error, as shown in the figure:
NuGet command line
In addition to the NuGet package manager, you can also use the NuGet command line to install.
First, open NuGet's official website: https://www.nuget.org/ , type the NuGet package keyword you want to find in the search box (Newtonsoft is shown here), and click the search , as shown in the figure:
Click the package that meets the requirements in the search result list ( Newtonsoft.Json ), as shown in the figure:
Enter Newtonsoft.Json , this page displays the detailed information of the package, such as: command line, dependency, list of used by other projects, historical version list, basic information, etc., as shown in the figure:
Package Manager console commands
Install NuGet package
First, demonstrate the package manager console and copy the package manager console commands:
Install-Package Newtonsoft.Json -Version 13.0.1
In Visual Studio, open the package manager console , as shown in the figure:
Paste the command in the package manager console , and set the default project as the current project (if there are multiple projects in a solution, you need to carefully check the project selected in this drop-down box), press Car key to execute the command.
The command execution result is shown in the figure:
program package manager console command mode successfully installed the NuGet package.
Update NuGet package
In the package manager console , you can also update the NuGet package.
You can get to check whether there is a new version of the installed package of the current project, the command is as follows:
Get-Package -updates
Update the specified package to the specified version, the command is as follows:
Update-Package Newtonsoft.Json -Version 13.0.1
Update the specified package of the project to the specified version, the command is as follows:
Update-Package Newtonsoft.Json -Version 13.0.1 -ProjectName ConsoleApp1
Update all packages of the current solution to the latest version, the command is as follows:
Update-Package
Uninstall NuGet package
In the package manager console , you can also uninstall the NuGet package.
Uninstall the specified package of the default project, the command is as follows:
Uninstall-Package Newtonsoft.Json
Uninstall the specified package of the default project and uninstall unused dependent packages at the same time, the command is as follows:
Uninstall-Package Newtonsoft.Json -RemoveDependencies
To force uninstall the specified package of the default project (even if other packages depend on it), the command is as follows:
Uninstall-Package Newtonsoft.Json -Force
.NET CLI to manage NuGet packages
To use the .NET CLI tool, you need to install the .NET Core SDK. If you have installed Visual Studio 2017 and above, the .NET CLI tool will be installed automatically.
When using the command line to manage NuGet packages, please locate the root directory where the project is located on the command line.
Install Nuget package
The command to Newtonsoft.Json package using the .NET CLI is as follows:
dotnet add package Newtonsoft.Json --version 13.0.1
The command is executed as follows:
Uninstall Nuget package
The command to uninstall the Newtonsoft.Json package is as follows:
dotnet remove package Newtonsoft.Json
The next article introduces "Building a lightweight private Nuget package server based on BaGet in 1 minute"
If you have any questions, please leave a message in the comment area.
If you think this article is valuable, please come to Sanlian (like, favorite, comment), thank you.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。