Example code for creating and using nuget package in. Net core

Time:2020-9-1

In. Net core projects, if we want to reference other DLL files in the project, it is not recommended to directly add DLL files to the project references (although this can also be done in. Net core projects). It is recommended to download the nuget package to which the DLL files belong. The biggest advantage of this is that we can introduce all the dependent files of DLL files into the project, which ensures the integrity of the reference files and makes them run correctly.

Let’s go through a. Net core class library project and a ASP.NET Core project to demonstrate how to publish a nuget package and reference the nuget package in the project.

First, we create a new. Net core class library project“ Custom.Library.StringUtils It contains a simple string tool class “stringhelper” for simple string processing

using System;
using Newtonsoft.Json;

namespace Custom.Library.StringUtils
{
 /// <summary>
 ///String utility class
 /// </summary>
 public static class StringHelper
 {
  /// <summary>
  ///Converts a string to uppercase
  /// </summary>
  ///< param name = "text" > string < / param >
  ///< returns > uppercase strings < / returns >
  public static string GetUpperString(string text)
  {
   return text.ToUpper();
  }

  /// <summary>
  ///Convert an object to a JSON string
  /// </summary>
  ///< param name = "jsonobject" > object < / param >
  ///< returns > JSON string < / returns >
  public static string ToJson(object jsonObject)
  {
   return JsonConvert.SerializeObject(jsonObject);
  }
 }
}

Note that in order to demonstrate that our. Net core class library project also references other nuget packages, we refer to and use the famous JSON processing nuget package in the project Newtonsoft.Json

Next, we will put the class library project“ Custom.Library.StringUtils “Publish as a nuget package.

First of all, because we wrote a lot in the project<summary>Note, we certainly hope that other. Net core projects will also get these after referencing our nuget package<summary>Therefore, we need to make some settings on the project properties

On the properties of the project, we select the build page. There are two places to set. First of all, because we want to generate an XML documentation file in the published nuget package to contain < summary > comments, we need to select the configuration drop-down box as all Configurations so that we can also generate XML documentation files at release time, not just during debugging. Then we need to make sure that the XML documentation file check box is checked, so that the corresponding XML documentation file will be generated after our. Net core class library project is published:

Secondly, each nuget package actually has a version number. On the package page, we can set the version number of the current nuget package to 1.0.0 through package version, as shown below:

Then we can publish our. Net core class library project“ Custom.Library.StringUtils “For nuget, click publish on the project:

In the pop-up configuration window, select a publish directory. Here, we choose the local computer directory “C:” local nuget repository. This directory will also be the source of our local nuget package manager. In fact, we can also select a shared directory of a remote computer (for example, \ \ 192.168.1.105 \ \ remote nuget) As the source directory of nuget package, if our development team is in a LAN, we can let visual studio of each developer get the required nuget package from the shared directory of LAN.

After clicking publish, we can see that the nuget file generated by the class library project already exists in the directory “C: local nuget repository”

Note that the generated nuget file name contains the version number. For example, if we change the package version to 2.0.0 on the package page in the project properties, as shown below:

Then publish the. Net core class library project. We can see that a nuget file will be generated under the publishing directory “C: local nuget repository”. This time, the version number is 2.0.0:

Next we’re going to be in another one ASP.NET The nuget package we created is referenced in the core project, so we create a new one ASP.NET Core project “aspnetcore 3000message”.

First, we need to configure the source of nuget package in visual studio, and select Tools > nuget package manager > package manager settings in visual studio

Then, select the package sources page in the pop-up window, and then click the plus sign to create a new nuget package source with the name of “local nuget package” and the path (source) of the. Net core class library project published in the previous “C: local nuget repository”, as shown below:

After clicking the OK button, we will set a local directory as the source of nuget package manager in visual studio. As mentioned above, we can also use the shared directory of a remote computer as the source, so that visual studio of the development team can get nuget packages from the shared directory.

Now we are ASP.NET In the core project “aspnetcore 3000message”, open nuget package manager:

Then, we select all from the package source drop-down box in the upper right corner, and enter the name of the. Net core class library project we published earlier in the search box“ Custom.Library.StringUtils “, we can find the nuget package that we released, because we have previously released two versions to the release directory” C: “local nuget There are also two versions of nuget package, which are “1.0.0” and “2.0.0”, because we are in. Net Core class library project“ Custom.Library.StringUtils “Is quoted in Newtonsoft.Json This nuget package, so we can see that our nuget package also depends on Newtonsoft.Json To install our nuget package Custom.Library.StringUtils Will also be downloaded and installed Newtonsoft.Json This nuget package ensures that all dependent DLL files are referenced here we are ASP.NET In the core project “aspnetcore 3000message”:

Here we choose to install version 2.0.0:

After that, we can ASP.NET The stringhelper class in the. Net core class library project is used in the core project, and you can seeThe < summary > comments are displayed successfully:

reference:

How to include XML documentation file in NuGet package built from a project file?

This article on the. Net core to create and use nuget package example code to introduce this, more related to. Net core to create and use nuget package content, please search the previous articles of developeppaer or continue to browse the related articles below, I hope you can support developeppaer more in the future!