Each project will need its own dependencies from external libraries or references to another project in the same solution.
The packages that a project references are described in the project’s csproj
file.
NuGet is a package manager for .NET
NuGet Gallery is the central package repository for .NET libraries You can find its directory here
Let’s add a dependency to a project by using an example package Newtonsoft.Json
which is a popular
Json framework for .NET that is published in the central repository
# If inside the project directory
dotnet add package Newtonsoft.Json
# If in root directory of a multi-project solution
dotnet add <path-to-project> package Newtonsoft.Json
The previous command should be run within the project folder that you want to add the dependency to.
An example of external packages that a project uses within the project’s csproj
file
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3-beta1" />
</ItemGroup>
When you clone a fresh copy of the codebase, or if you pull in new changes from Git that involve added dependencies, you will want to restore (aka fetch) dependencies on your local development machine.
dotnet restore
If you want to add a reference of one project to another, in the case of needing to use the resources of another project, you can do so:
dotnet add [path-to-project] references [path-to-project-to-be-referenced]
# e.g. add source project reference to a test project
dotnet add tests/NeverendingTeaShop.API.Tests/NeverendingTeaShop.API.Tests.csproj reference src/NeverendingTeaShop.API/NeverendingTeaShop.API.csproj
An example of a project reference that was created:
<ItemGroup>
<ProjectReference Include="..\..\src\NeverendingTeaShop.API\NeverendingTeaShop.API.csproj" />
</ItemGroup>