The fundamentals of development: NuGet Packages

Enjoy Digital
By Enjoy Digital
5 minutes to read

NuGet is the default tool that allows .NET developers to easily share and use code.

It can be used on loads of projects including websites that use Json.NET, connecting databases with Entity Framework, and even code from the development community like jQuery and Angular. NuGet is fundamental to projects and can help encourage collaboration and skills development.

The practical uses of NuGet packages

There are two practical uses for the packages we generate:

  1. When used in combination with TeamCity, deployment packages are automatically generated so your site’s code is continuously up-to-date.
  2. NuGet packages contain common tools that we use as a base for our Umbraco sites. This is useful for countless reasons so we try and keep it as up-to-date with Umbraco versions as we possibly can and feedback improvements on the many projects we work on.

So, without further ado, here’s how to set up your own NuGet package, stored on a local file system so it’s not publically available. If you do want to make a public package, visit the host site (like for upload instructions.

We’re working on the assumption that you already have a basic library of code. It could be a DLL or a website but the rules are the same for either.

How to set up a NuGet package

  1. Download nuget.exe from:
  2. Create a manifest file with the .nuspec extension. This describes the contents of the package, as well as any other packages it depends on.

This file can be automatically generated using the "nuget spec" command in the project’s root directory. The structure of the file will depend on several things like the structure of the project or if there’s a .csproj or .vbproj file present. Here’s a simplified example:

<?xml version="1.0"?>
    <authors>Enjoy Digital</authors>
    <owners>Enjoy Digital</owners>
    <description>Example NuGet Package</description>
    <copyright>Copyright © 2018</copyright>
  1. To determine which files should be included in the project, specify them in the "files" node:
  <file src="readme.txt" target="" />
  1. Finally, call the "nuget pack '<package-name>.nuspec'" command to build the package.

Modifying your package

These steps generate a .nupkg file that can be placed wherever you want to reference it. However, there are a few modifications we may still want to make. If we’re using this as a base for a website build, we may want to replace part of the namespace in the .cs files to match the name of the project we’re installing the package into.

To do this we need to create a .pp file. For example, you may name a .pp file Utility.cs.pp to correspond with your utility.cs file. The .pp would have a simple substitution in the namespace line:

namespace BaseSite.Utility

namespace $rootnamespace$.Utility

The $rootnamespace$ token will be replaced when installed in the target project with the corresponding value in that project’s property file.

You can see how we might have to create a .pp file for every .cs file in the project. Thankfully, there’s a sanity-saving NuGet package – NuGet Content Generator – which automatically generates on build simply by including appropriate headings for each file.

We can then include these .pp files in the manifest, removing any references to the corresponding .cs files, and regenerate the .nupkg file.

Consuming the package

So, our package is all set but how do we consume it? The easy way is to upload it to or another host service. But that’s not an option if we want to keep it private or in-house.

That means we need to host it on a local file system or on one of your network drives. All you need to do is copy the .nupkg file to your source of choice. You can then add this to a list, either in Visual Studio’s NuGet Package Manager settings (found in the options window and is added by specifying a name and source url/folder) or with the command line: "nuget sources Add -Name 'local packages' -Source <filepath>".

And that’s it! You can both create your own package and consume it from a private feed. All that’s left to do is ensure you update the version number whenever you make changes to ensure your projects pick up the update.

Happy coding!

comments powered by Disqus