Dotnet Core 简明教程

.NET Core - Sharing Libraries

在本节中,我们将探讨如何将您的库作为 NuGet Package 共享,以便在其他项目中使用。创建包从希望打包并与其他人共享的代码开始,可以通过公用 nuget.org 图库或组织内的专用 图库进行共享。包还可以包括其他文件,例如在安装包时显示的 readme ,并且可以对某些项目文件进行转换。

In this chapter, we will discuss how to share your library as NuGet Package so that it can be consumed within another project. Creating a package starts with the code you want to package and share with others, either through the public nuget.org gallery or a private gallery within your organization. The package can also include additional files such as a readme that is displayed when the package is installed, and can include transformations to certain project files.

现在,让我们考虑一个简单的示例,其中我们将从库中创建一个 NuGet 包。为此,请打开命令提示符并转到库项目的 project.json 文件所在的文件夹。

Let us now consider a simple example in which we will create a NuGet package from our library. To do so, open the command prompt and go to the folder where the project.json file of your library project is located.

example

现在,让我们运行以下命令。

Let us now run the following command.

dotnet help
command

最后,您可以看到诸如 new、restore 和 build 之类的不同命令。

At the end, you can see different commands like new, restore and build, etc.

最后一个命令是 pack; 这将创建一个 NuGet 包。现在,让我们执行以下命令。

The last command is pack; this will create a NuGet package. Let us now execute the following command.

dotnet pack
execute

您现在可以看到 NuGet 包已在 bin 文件夹中生成;让我们打开 bin\Debug 文件夹。

You can now see that the NuGet packages are produced in the bin folder; let us open the bin\Debug folder.

debug folder

现在的问题是 NuGet 包中包含什么内容,要了解这一点,我们可以使用 NuGet 包资源管理器。现在,让我们打开 NuGet 包资源管理器。

Now the question is what is inside the NuGet packages, to see that we can use NuGet Package Explorer. Let us now open the NuGet Package Explorer.

open nuget

选择第一个选项 Open a local package

Select the first option Open a local package.

first option

选择 StringLibrary.1.0.0.nupkg 并单击 Open

Select the StringLibrary.1.0.0.nupkg and click Open.

click ok

你可以在“程序包内容”部分中看到我们只包含了 StringLibrary.dll。在“程序包元数据”部分中,你将看到有关此程序包的一些信息,例如 Id、版本和所有依赖项。

You can see that in the Package contents section we have StringLibrary.dll only. In the Package metadata section, you will see a bit of information about this library like Id, Versions and all the of the dependencies.

我们现在打开 StringLibrary.1.0.0.symbols.nupkg

Let us now open the StringLibrary.1.0.0.symbols.nupkg.

symbols

在此 NuGet 程序包中,你会看到源文件以及 *.pdb 文件。如果你在 StringLib.cs 文件上双击,你还可以看到源代码。

In this NuGet package, you will see the source files and the *.pdb file as well. If you double-click on the StringLib.cs file, you see the source code as well.

stinglib cs

这里的问题是,如何配置元数据(例如版本、作者和描述)等。

Here the question is, how can configure the metadata like version, authors and description, etc.

project.json 文件用于 .NET Core 项目,目的是定义项目元数据、编译信息和依赖项。我们现在打开 project.json 文件,并添加以下额外信息。

The project.json file is used on .NET Core projects to define project metadata, compilation information, and dependencies. Let us now open the project.json file and add the following additional information.

{
   "authors": [ "Mark Junior" ],
   "description": "String Library API",
   "version" : "1.0.1-*",
   "supports": {},

   "dependencies": {
      "Microsoft.EntityFrameworkCore": "1.1.0",
      "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
      "NETStandard.Library": "1.6.0",
      "System.Runtime.Serialization.Json": "4.0.3",
      "System.Runtime.Serialization.Primitives": "4.3.0"
   },
   "frameworks": {
      "netstandard1.3": {}
   }
}

你现在可以看到其他信息,如作者名称、描述和此处添加的版本。我们保存此文件,构建程序包项目,然后再次执行“dotnet pack”命令。

You can now see additional information like author name, description and version added here. Let us save this file, build the library project, then execute the “dotnet pack” command again.

dotnet pack

在 bin\Debug 文件夹中,你可以看到 StringLibrary NuGet 程序包已使用 1.0.1 版本生成;我们已在 NuGet Package Explorer 中将其打开。

Inside the bin\Debug folder, you can see that the StringLibrary NuGet packages are produced with version 1.0.1; let us open it in NuGet Package Explorer.

version

你将看到更新的元数据。现在的问题是,我们如何在另一个程序包中使用它。

You will see the updated metadata. The question now is, how can we use it in another package.

我们需要先在 NuGet 源中发布它,然后我们才能在另一个项目中使用它。

We need to start by publishing somewhere in the NuGet feed and then we can consume it in another project.

发布更新的元数据有两种选择:

There are two options to publish the updated metadata −

  1. Publish it to nuget.org

  2. Push the metadata to private NuGet feed

此处我们将使用私有 NuGet 源,因为它比在 nuget.org 上设置帐户容易得多。要了解如何将包发布到 nuget.org,你可以按照此处 https://docs.microsoft.com/en-us/nuget/create-packages/publish-a-package 指定的所有准则进行操作。

Here we will be using the private NuGet feed because it is a lot easier than to setup an account on nuget.org. To learn how to publish your package to nuget.org, you can follow all the guidelines specified here https://docs.microsoft.com/en-us/nuget/create-packages/publish-a-package.

按照以下步骤将更新的元数据推送到私有 NuGet 源。

Follow these steps to push the updated metadata to private NuGet feed.

Step 1 - 首先,我们需要 nuget 命令行实用工具,并且我们必须将其安装。我们现在打开 NuGet Package Manager,并搜索 nuget.commandline。

Step 1 − To start with, we need the nuget commandline utility and we have to install it. Let us now open the NuGet Package Manager and search for nuget.commandline.

Step 2 - 选择 Nuget.Commandline,然后单击 Install

Step 2 − Select Nuget.Commandline and click Install.

commandline

Step 3 - 单击 OK 以安装 Nuget.Commandline。你还可以通过从以下网址 https://dist.nuget.org/index.html 下载它并设置环境变量来手动安装它。

Step 3 − Click OK to install Nuget.Commandline. You can also manually install it by downloading it from the following Url https://dist.nuget.org/index.html and then set up the environment variable.

manual install

Step 4 - 安装完成后,我们重新打开命令提示符并转到 bin\Debug 文件夹(NuGet 程序包位于此文件夹中),并指定以下命令:

Step 4 − Once the installation is finished, let us open the command prompt again and go to the bin\Debug folder where the NuGet packages are located and specify the following command −

nuget add StringLibrary.1.0.1.nupkg -Source D:\PrivateNugetPackages

Step 5 - 在以上命令中,我们将 StringLibrary.1.0.1.nupkg 程序包添加到我们的私有源,位置为 D:\PrivateNugetPackages , -Source 指定包源。

Step 5 − In the above command, we add the StringLibrary.1.0.1.nupkg package to our private feed and the location is D:\PrivateNugetPackages, -Source specifies the package source.

Step 6 - 你可以看到已安装 StringLibrary ;可以将 StringLibrary 进一步添加到私有源。

Step 6 − You can see that the StringLibrary is installed; the StringLibrary can further be added to the private feed.

private feed

Step 7 − 让我们转到该文件夹。

Step 7 − Let us go to that folder.

folder

Step 8 − 在 stringlibrary 文件夹的内部,你会看到另一个具有版本名称的文件夹,此处为 1.0.1。

Step 8 − Inside the stringlibrary folder, you will see another folder with the version name and here it is 1.0.1.

version name

NuGet 包位于此处。

The NuGet package is located here.