For .NET projects, there is NuGet, the package manager to fulfil the external dependencies in the project. It’s not just limited to the packages that are present on http://www.nuget.org, but there can be multiple package repositories. That’s all nice, but what if you have some home-grown library that you do not want to make publically available? A great use of NuGet packages can be done for class reusability in private projects as well. It is easy to create a NuGet repo on local machine or FTP shared location on your network. How? I’ll show you.
Create a .NET Standard class library for the classes to be shared as a package.
Add your code and public API classes to this library.
public class MathUtils
public static int Square(int number) => number * number;
Go to project properties > Package. Fill assembly details. Check on Generate NuGet package on build. Save.
After completing the project, pack it to create the nupkg file. Technically speaking, a NuGet package is just a ZIP file that’s been renamed with the .nupkg extension and whose contents match certain conventions. To pack a project, build it and then right click it in Solution Explorer and click Pack.
You’ll see the package is created in debug or release folder.
Alternatively, we can use MSBuild to pack (for build scripts) as follows —
msbuild /t:pack /p:Configuration=Release
Make sure NuGet CLI is installed from www.nuget.org and is saved in PATH environment variable. Then create a folder accessible to you (local directory or network share) as package repo.
Publish the package to local repo using ‘nuget add’ —
>nuget add VHC.StdLib.1.0.0.nupkg -source d:\source\nuget_repo
Now, let’s take a look at the repository nuget_repo. The package is listed as per version, so it is important to maintain assembly versions while publishing.
Similarly, ‘nuget delete’ is used to delete a package from repo.
Now, we are ready to consume our library from another project. Let’s create another client project using Visual Studio. Right click on the project, and go to Manage NuGet Packages for Solution…
Click the Settings button (little gear icon) next to the Package Source.
We find ourselves in Visual Studio Options for NuGet package sources. These are all the places NuGet looks for dependencies. Let’s add our nuget_repo folder here. Add a new package repo using (+) button and set the path to our source folder. Make sure this one is used before nuget.org. You can arrange the sources by arrow buttons on top right. Click OK when done.
Now, our package can be found and installed via NuGet.
OR it can be installed using Package Manager Console.
This can then be used like any other package.
For more in-depth information on this, please refer to the MSDN sources which I learned this from —
NuGet Command-Line Interface (CLI) Reference
Command-line reference index for the nuget.exe CLI
Introductory Guide to Creating and Publishing a .NET Standard NuGet Package using Visual Studio
A walkthrough tutorial on creating and publishing a .NET Standard NuGet package using Visual Studio 2017.
NuGet Package Manager UI Reference
In this article The NuGet Package Manager UI in Visual Studio on Windows allows you to easily install, uninstall, and…
NuGet Gallery | Home
NuGet is the package manager for .NET. The NuGet client tools provide the ability to produce and consume packages. The…
Thank you for reading. Please clap to make this article more discoverable.