Creating and publishing NuGet-packages
This will be one of the first posts I’ve done in a while. I’ll try keeping them relatively brief, so we can bump the quantity instead. Keeping them to the point will be my goal and if you feel a post has helped, I’d be super appreciative knowing in the comments below (criticism welcomed too!). This will be pretty basic, so if you know about NuGet, how to create and deploy them, there likely won’t be much for you to learn here.
With all that said, lets first ask the question: what is a NuGet-package? Very simple, it’s a container with components inside, designed for re-usability and sharing. It can contain a class libraries or even an entire project including the .cs files. It’s then encapsulated into a redistributable package for anyone to use. It doesn’t matter if it’s your personally written library with a couple of methods, or the entire EntityFramework, anybody can encapsulate their project into a NuGet and there are thousands of brilliant libraries and frameworks out there for you to freely use.
Now hopefully your own components will be well designed for future re-usability, so the next person to put it to use can do so without trouble, but that is besides the point and this guide will simply be how we create the package and publish it for accessibility. Don’t worry, it’s super easy! What you need:
- A class library or other project you wish to create a NuGet-package from.
- The “NuGet Package Explorer” program. This is basically a GUI to allow the creation of a NuGet-pack stupidly simple (still a powerful tool when it needs to be). Understand a NuGet contains a “.nuspec” configuration file which would normally take manual editing, but the Package Explorer tool automates it making it VERY easy for us.
- A nuget.org account for an API-key. No one forces you to publicly share your NuGet-packages, you can even host your own internal repository-server, however, nuget.org is by default used in Visual Studio meaning that anyone can very quickly locate and install your package without additional configuration.
First things first, we want to build/compile our class library to acquire the magic DLL. Once ready, find your projects “bin\Debug” folder for the DLL(s). Open up the NuGet Package Explorer and you can simply drag and drop the DLL(s) to the package contents window. The program will intelligently attempt to infer what you are trying to package, and make a suggestion. Since we are attempting to package a dll-library, the tool does its thing:
You may want to update the metadata, such as the Id of your package, but finally you go to File -> Publish. Use your API key you find on the nuget.org site, under your account, and it shouldn’t take long before your package is ready for use by anyone in the world with a few clicks. So for example, if you wish to add my published test package; just right-click a solution and go to “Manage NuGet Packages”, go to the “Browse” tab and search for “Amivit”. You will find the very example I experimented with prior to this post, ready to be implemented in your own project.
… and that’s all! I’d say it is very beneficial being comfortable with these possibilities, so give it a try, it’s super easy as you can see.