🐎BLOG🐎

4: Microsoft Tutorialノート

pulished:

Learning objectives

Add package to your .NET project

.NET and its ecosystem use the word dependency a lot. A package dependency is a third-party library. It’s a piece of reusable code that accomplishes something and can be added to your application. The third-party library is something your application depends on to function, hence the word dependency.

You can think of the third-party library as a package, and it’s stored in a repository. A package consists of one or more libraries that you can add to your application so you can take advantage of its features.

Determine whether you need a package

How do you know if you need a package for your project? That’s a complicated question that involves a few factors:

Evaluate a package

You can learn more about a package before installing it by going to https://www.nuget.org/packages/<package name>

To ensure that you can use a package, all dependencies are crawled and downloaded when you run the dotnet add package <package name> command.

Install a package

There are several ways to install packages. There’s a built-in command line and graphical user interface for a package manager in Visual Studio and Visual Studio for Mac. You can manually add package references to your project file, or you can install them through a command-line interface (CLI) tool such as Paket or the .NET Core CLI.

A typical installation command looks like this one: dotnet add package <name of package>.

After your project installs and builds, the references are added into your debug or release folders. Your project directory looks something like this one:

-| bin/
---| Debug/
------| net3.1
--------| <files included in the dependency>

Find a package

Nuget.org

Packages might be located in many different places. Some of these sources might be publicly available, and some might be restricted and available only to employees of a specific company. Here are some places where packages might reside:

Nuget Package

.NET commands

To help you remember what the commands do, it helps to think of them as belonging to categories:

dotnet --help も便利。

How to install a package

dotnet add package <dependency name>

💡Note:

You can install some packages globally. These packages aren’t meant to be imported into your project. For that reason, many global packages are CLI tools or templates. You can also install these global tools from a package repository. Install tools by using the dotnet tool install <name of package> command. Install templates by using the dotnet new -i <name of package> command.

After installation

The installed packages are listed in the dependencies section of your .csproj file. If you want to see what packages are in the folder, you can enter:

dotnet list package

Output will be:

Project 'DotNetDependencies' has the following package references
   [net5.0]:
   Top-level Package      Requested   Resolved
   > Humanizer            2.7.9       2.7.9

This command lists only the top-level packages, and not dependencies of those packages that we call transitive packages.

This is nice for a quick look. If you want a more in-depth view, you can list all transitive packages. When you do so, the list command looks like this one:

dotnet list package --include-transitive

Output will be:

Project 'DotNetDependencies' has the following package references
   [net5.0]:
   Top-level Package      Requested   Resolved
   > Humanizer            2.7.9       2.7.9

   Transitive Package               Resolved
   > Humanizer.Core                 2.7.9
   > Humanizer.Core.af              2.7.9
   > Humanizer.Core.ar              2.7.9
   > Humanizer.Core.bg              2.7.9
   > Humanizer.Core.bn-BD           2.7.9
   > Humanizer.Core.cs              2.7.9
   ...

Restore dependencies

When you create or clone a project, the included dependencies are not downloaded or installed until you build your project.

You can manually restore dependencies, as well as project-specific tools that are specified in the project file, by running the dotnet restore command. In most cases, you don’t need to explicitly use the command. NuGet restore runs implicitly, if necessary, when you run commands like new, build, and run.

Clean up dependencies

Sooner or later, you’re likely to realize that you no longer need a package, or you might realize that the package you installed isn’t the one you need. Maybe you’ve found one that will accomplish a task better. Whatever the reason, you should remove dependencies that you aren’t using. Doing so keeps things clean. Also, dependencies take up space.

To remove a package from your project, use the remove command like so:

dotnet remove package <name of dependency>. This command will remove the package from your project’s**.csproj file**.

Create a sample .NET project

To set up a .NET project to work with dependencies, we’ll use Visual Studio Code. Visual Studio Code includes an integrated terminal, which makes creating a new project easy. If you don’t want to use another code editor, you can run the commands in this module in a terminal.

  1. In Visual Studio Code, select File > Open Folder.
  2. Create a new folder named DotNetDependencies in the location of your choice, and then select Select Folder.
  3. Open the integrated terminal from Visual Studio Code by selecting View > Terminal from the main menu.
  4. In the terminal window, copy and paste the following command.
    1. dotnet new console -f net6.0
  5. In the terminal window, copy and paste the following command to run the “Hello World” program.
    1. dotnet run

Manage dependency updates in your .NET project

Sooner or later, you’ll want to update to a new version of a library. Maybe a function is marked as deprecated, or maybe there’s a new feature in a later version of a package you’re using.

Take these considerations into account before you try to update a library:

Configure the project file for update

NotationApplied ruleDescription
1.0x >= 1.0Minimum version, inclusive
(1.0,)x > 1.0Minimum version, exclusive
[1.0]x == 1.0Exact version match
(,1.0]x ≤ 1.0Maximum version, inclusive
(,1.0)x < 1.0Maximum version, exclusive
[1.0,2.0]1.0 ≤ x ≤ 2.0Exact range, inclusive
(1.0,2.0)1.0 < x < 2.0Exact range, exclusive
[1.0,2.0)1.0 ≤ x < 2.0Mixed inclusive minimum and exclusive maximum version
(1.0)invalidinvalid