On this page

Skip to content

Developing with Local NuGet in Visual Studio in Offline Environments

Introduction

In some scenarios, development environments may have restricted access to the internet. Consequently, when using packages, one might need to manually download the corresponding DLLs for reference. However, since some packages are released for multiple versions of the Framework, the DLLs on hand may not be suitable for the current Framework. Additionally, some packages may depend on others, requiring all relevant DLLs to be referenced to function correctly.

Directly referencing DLLs makes it difficult to effectively manage packages via NuGet when handing over the project to other developers who use NuGet. Therefore, in such scenarios, it is recommended to avoid directly referencing manually downloaded DLLs.

Furthermore, if the inherited project uses NuGet to install packages and development is conducted in an offline environment, you may face issues related to package restoration.

NuGet Offline Package Configuration

In the "Package Sources" settings of Visual Studio, you can see that there are already default settings related to offline packages.

vs nuget offline package source

By default, some packages exist under the configured folder.

offline nuget files

You can also click the "+" button in the top right corner to add your own sources.

add local nuget source

When installing packages using NuGet, you can select the package source in the top right corner.

select local nuget source

Extending Local Packages

For information on how to publish a Class Library as a package, please refer to Publishing NuGet Packages with Default Files using Visual Studio; this will not be covered here.

There are two ways to publish local packages to a local folder:

  1. Download "nuget.exe" from NuGet Downloads.
  2. Install the "NuGet.CommandLine" package for NuGet, then use the "Package Manager Console" to perform operations (essentially, this also operates "nuget.exe", which is downloaded along with the installation of "NuGet.CommandLine").

Next, use the nuget add {packagePath} -Source {sourcePath} command to publish the package to {sourcePath}. For specific command details, please refer to add command (NuGet CLI).

Extending Packages from NuGet

This method requires a computer that can connect to NuGet and has permission to transfer files to the development environment.

When installing a package from NuGet, the package containing NuGet data is downloaded locally. In a Windows environment, the path is "%userprofile%.nuget\packages". When subsequent projects need to install the same package, they will attempt to install it from the local machine first. If the package does not exist locally or the specified version cannot be installed, it will then attempt to install from the configured package source.

Therefore, you simply need to place the packages you want to use into the offline package source configured for the development environment. In other words, if you want to control the packages or versions available in the development environment, authorized personnel can install the packages first and then place them in the respective development environments for developers to use.

WARNING

When installing NuGet packages using .NET Framework, "packages.config" is used by default. In this case, there will be a "packages" folder in the project root directory, which also contains the installed packages. However, these packages do not contain complete NuGet data, so do not copy them from here to the configured local package source.

Change Log

  • 2023-12-05 Initial document created.