On this page

Skip to content

Developing with Local NuGet in Visual Studio in Offline Environments

TLDR

  • When developing in an offline environment, avoid direct DLL references; use local NuGet package sources instead for better version management.
  • You can add a local folder as a NuGet source via Visual Studio's "Package Sources" settings.
  • Use the nuget add command to publish custom .nupkg packages to a designated local source folder.
  • If external NuGet packages are required, obtain them from a computer with internet access and place them into the local source folder in the offline environment.
  • Never copy the contents of the packages folder under a packages.config project directory directly into a NuGet source, as it lacks the necessary NuGet metadata.

Why Configure a Local NuGet Source

In a closed development environment without internet access, manually downloading and referencing DLLs leads to version management difficulties and makes handling package dependencies challenging. If a project is managed via NuGet, package restoration will fail in an offline state. By configuring a local NuGet source, you can ensure the development environment has a stable package repository and resolve issues with inconsistent DLL versions.

Configuring a Local NuGet Package Source

When does this issue occur: When the development environment cannot access the internet and you need to install or restore NuGet packages.

In Visual Studio, you can configure a local source by following these steps:

  1. Open the NuGet Package Manager settings in Visual Studio.
  2. In the "Package Sources" option, you can see the default offline sources.
  3. Click the "+" button in the top right corner to add the specified local folder path to the source list.

![vs nuget offline package source](../../../devops/images/在離線環境中,在 Visual Studio 中使用本機 NuGet 開發/vs-nuget-offline-package-source.png)

![add local nuget source](../../../devops/images/在離線環境中,在 Visual Studio 中使用本機 NuGet 開發/add-local-nuget-source.png)

Once configured, you can switch to this local path in the source menu at the top right when installing packages.

![select local nuget source](../../../devops/images/在離線環境中,在 Visual Studio 中使用本機 NuGet 開發/select-local-nuget-source.png)

Extending Local Packages

When does this issue occur: When you need to provide a self-developed Class Library as a NuGet package for use in an offline environment.

To publish a package to a local folder, use nuget.exe to execute the following command:

bash
nuget add {packagePath} -Source {sourcePath}
  • {packagePath}: The path to your .nupkg file.
  • {sourcePath}: The path to the local NuGet source folder you configured.

Extending Packages from NuGet

When does this issue occur: When a project needs to use publicly available NuGet packages from the internet, but the development environment cannot connect to download them.

This method requires a computer with internet access to act as an intermediary:

  1. Install the required packages on a computer with internet access.
  2. Locate the package cache path in Windows (usually located at %userprofile%\.nuget\packages).
  3. Copy the package files and place them into the local source folder configured in the offline development environment.

WARNING

When installing NuGet packages in .NET Framework, the default is to use "packages.config". In this case, there will be a "packages" folder in the project root containing the installed packages. However, these packages do not contain complete NuGet metadata, so do not copy them from here into your configured local package source.

Change Log

  • 2023-12-05 Initial document creation.