Dotnet Core 简明教程
.NET Core - Package References
在本章中,我们将讨论如何在 .NET Core 应用程序中添加包以及如何查找特定的包。我们可以直接转到 NuGet 并添加包,不过我们将在其他一些地方看到。
In this chapter, we will discuss how to add packages in your .NET Core application and how to find a specific package. We can directly go to NuGet and add package, but here we will see some other places.
现在让我们转到 .NET Core 的源代码,其位于此处 - https://github.com/dotnet/corefx
Let us now go to the source code of .NET Core which is located here − https://github.com/dotnet/corefx
在 CoreFx 存储库中,打开 src 文件夹 -
In CoreFx repo, open the src folder −
你将看到一整套与不同程序包相对应的文件夹。我们现在搜索 Json -
And you will see the whole list of folders that correspond to different packages. Let us now search Json −
有一种方法可以查找软件包,如果您熟悉 .NET Framework,可能知道各种类型,但是 .NET Core 中的软件包组装完全不同,您不知道其中的软件包位于何处。
There is another way to find your package, you probably know various types if you are familiar with .NET Framework, but the assembling of packages in .NET Core is totally different and you won’t know where that packages in.
如果您知道类型,您可以使用 https://packagesearch.azurewebsites.net/ 搜索反向软件包搜索
If you know the type, you can search to reverse package search by using https://packagesearch.azurewebsites.net/
在此处,您可以输入希望查找的任何类型的软件包。然后,此站点将扫描 NuGet 并为您找到相关软件包。
Here you can enter any type of package you would like to find. Then, this site will scan NuGet and find the relevant packages for you.
现在让我们搜索 DataContractJson 。
Let us now search for DataContractJson.
您现在会看到,我们得到了相同的软件包;让我们单击软件包。
You will now see that we get the same package; let us click on the package.
您现在将看到 NuGet 页面;您需要确认需要此软件包。可以使用一些方法在应用程序中添加此软件包。
You will now see the NuGet page; you need to confirm that you need this package. You can add this in your application using a few methods.
让我们打开 project.json 文件。
Let us open the project.json file.
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
这是新的项目格式,此文件内将看到依赖项部分。让我们如下所示添加新的依赖项。
This is the new project format and inside this file you will see the dependencies section. Let us add a new dependency as shown below.
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
},
"System.Runtime.Serialization.Json": "4.0.2"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
现在,如果您查看引用,您将看到 System.Runtime.Serialization.Json 软件包添加到项目中。
Now if you look at your references, then you will see that System.Runtime.Serialization.Json package is added to your project.
另一种方式是转到 NuGet Manager 并浏览您要添加的软件包。
Another way is to go to the NuGet Manager and browse the package you want to add.