Using locally stored NuGet packages

NuGet packages eases the reuse of components. A NuGet package can contain different kinds of resources, like libraries, or header files. These resources are packed in a NuGet package. You can use a package in your solution and refer to one of the resources in the package.

During development, it can be convenient to just create your NuGet package locally and test it locally. Therefore you don’t need to set up your own hosting environment, like for example Artifactory, you just can host your packages on a local disk. Although the scenario where you publish your package to an external host is rather described everywhere, the latter one is a scenario that is interesting as a workaround not having to publish your package you are developing directly to an external source.

We will look at how to define the content of your NuGet package, create it, store it locally and eventually consume it.

Defining the content of your NuGet package

To tell NuGet, what the content of your package should be, you first have to define a .nuspec-file containing the metadata for your package. You can do this from scretch, or create an initial nuspec-file via

nuget spec

This spec-file can contain many kinds of meta-data, like the version number or a project URL. Here you also need to define the files, you want to include in your package. For example the library (DLL- or LIB-file) or any header files in case of a library written in C++. These files you need to define in the <files> section within the manifest file.

An example of a manifest file can look like this:

<?xml version="1.0"?>
<package >
    <title>My library</title>
    <description>My test library</description>
    <file src="..\Output\Bin\" target="dll" />

With the target attribute of the file-element, you define the directory where the source of the file should be placed within the package. So in the example below, within the package there will be a directory called dll, which contains the file

Creating the NuGet package

When you want to create a NuGet package via the command line, you enter the following command:

nuget pack <.nuspec-file>

Now, all sources defined in the manifest file (the .nuspec-file) will be put into one NuGet package. A NuGet package has the extension "nupkg".

If you open the package in, for example an archive application like 7-zip, you will see directories when you defined a target for a file. Besides that, the archive also contains the data that is required for the NuGet environment.

Configuring the location where to get packages from

The Nuget.config file is used as the configuration file to let the NuGet environment know where to get the packages from. NuGet can be hosted both remotely as also locally.

Documentation of NuGet describes the scenario to use a source that is located on a network share. However, you can also use this scenario to use packages that are stored on your local disk. Therefore, you can simply copy your generated package to this directory and consume it within your project.

The configuration file that is used to define the NuGet resources, is located in the NuGet.config file, that is located under c:\Users\<username>\AppData\Roaming\NuGet\. When you open this file, you will see something like this:

<?xml version="1.0" encoding="utf-8"?>
    <add key="" value="" protocolVersion="3" />

To add a new local location that contains NuGet packages, add a new entry within packageSources. You should add the key attribute that contains a user friendly name to the source. Within value, you can define the location to the resource. Normally, this will be a environment hosted externally and can be accessed using the HTTPS-protocol. If you want to use a directory that is locally on your disk, simply provide the full path to this directory within the value element:

<add key="Local Disk" value="C:\Work\packages" />

Besides configuring the resources via this text file, you can also configure this file via Visual Studio. Within your solution, press the right mouse button and select “Manage NuGet Packages for Solution…”. Within the new shown tab, press below the text “Manage Packages for Solution” the settings-wheel. Within the new popup, add a new source:

Configuring the source of the NuGet Packages

Here I added a source with the name Local Disk and I pointed a local directory “C:\Work\packages” as a directory containing NuGet packages.

Consuming the package

Now in a project, you can start consuming the package you created. Within Visual Studio, right click on the project you want the new package to be consumed. Click on “Manage NuGet packages…”, a new window will occur. Within the browse tab, you should now be able to find your package when you put the name of your user friendly name in the Search tab. This user friendly name, is the same name as the Title you defined in the .nuspec-file.

The benefit of having a local resource

Now you created your own NuGet package and host it locally, it is easy to update the local package and play around with it just by overwriting the existing package within the resource location. This is most of the times quicker when testing, most of the bigger projects contains code reviews and other kinds of checks before a package can be hosted.

Posted in C#, C++ by Bruno at January 31st, 2022.
Tags: , , ,

Leave a Reply