Manage packages in dotnet applications
By using packages, third party logic can be used in dotnet applications
Packages declaration in csproj file
- The external packages used by a project are declared in its csproj file like the following
- The packages will be downloaded and linked as per the csproj package references during the restore or build process
Browse Nuget packages online
Nuget packages can searched online at https://www.nuget.org/PACKAGES
Manage packages from dotnet cli
Install / update a package
command - dotnet add <PROJECT_FOLDER> package <PACKAGE_NAME> -v <VERSION>
- The following command adds/updates a package in a project. If the package is already present, it will be updated to the latest version
dotnet add package Microsoft.Data.Sqlite
- Install a specific version using -v flag like the following
dotnet add package Microsoft.Data.Sqlite -v 8.0.10
List all packages in a project
command - dotnet list <PROJECT_FOLDER> package
Example:
dotnet list package
Remove a package
command - dotnet remove <PROJECT_FOLDER> package <PACKAGE_NAME>
Example:
dotnet remove package Microsoft.Data.Sqlite
Downloaded Packages folder location
The packages will be downloaded and kept in the global packages folder. The folder location is as follows
- Windows:
%userprofile%\.nuget\packages
- Mac/Linux:
~/.nuget/packages
Manage packages in VS code
- In the solution explorer, right click on the project and select “Add Nuget Package”
- Manage packages from “Packages” section of the “Dependencies” section
Manage packages in Visual Studio
To manage packages in Visual Studio, right click on a project and click “Manage Nuget Packages”
Install a package
- Go to the browse tab of Nuget Package Manager. Search and install the required packages
Update a package
In the “Installed” tab of the package manager, select the package and change the version as required
Remove a package
In the “Installed” tab of the package manager, select the package and uninstall it
Download packages for offline installation
Method 1 - Download packages from nuget.org
- Search for the required package in nuget.org and download the .npkg file as shown below
- The dependent packages are also to be manually downloaded. Hence this approach is more tedious
Method 2 - Download required packages with dotnet CLI
- In a machine that has access to internet, create a dotnet project with required dependencies and download the packages to a desired folder.
- For example, create a new console app with required packages with the following commands
dotnet new console
dotnet add package Microsoft.Data.Sqlite
dotnet add package Microsoft.Identity.Client
- Download the packages to a folder named
Packages
using the following command
dotnet restore --packages .\Packages
Install packages from a folder
In a machine without internet, the following methods can be used to install packages from a local folder
Method 1 - Using dotnet CLI
- Keep the downloaded packages in a folder, say “Packages”
- Open a command prompt in a folder of the solution or project and run the following command to install packages (declared in csproj file) from the folder “Packages”
dotnet restore -s .\Packages
- If the package to be installed is not present in the .csproj file, explicitly install the package from the folder “Packages” using the following command
dotnet add package Microsoft.Data.Sqlite -s .\Packages\
Method 2 - Using Visual Studio Nuget Package Manager
- Open the Nuget Package Manager for the project and add the “Packages” folder as a new package source as shown below
- Add adding the package source, select the folder as a package source in Nuget Package Manager and install the required packages from the folder
Video
The video tutorial for this post can be found here
References
- VS Code package management - https://code.visualstudio.com/docs/csharp/package-management
- install packages in visual studio - https://learn.microsoft.com/en-us/nuget/consume-packages/install-use-packages-powershell
- SQLite package docs - Overview - Microsoft.Data.Sqlite | Microsoft Learn
Comments
Post a Comment