Why create NuGet packages?
We all talk about how great code reuse is, but how many of us actually reuse our code – other than opening up an older project and copy-pasting code from it into our new projects?
Creating your own NuGet packages is a simple way to share code across projects.
These NuGet packages can be useful for both our hobby and our work projects.
Create the code for the NuGet package
- Decide on the code you want to re-use and share
- Some functionality you repeatedly perform
- Something you wrote to simplify other development
- Create a Class Library project
- Lowest version of .NET possible (use .NET Standard, if possible)
- Determines where you can use this NuGet package
- Add code
- Add dependent packages, if needed
- Use lowest version possible
- If pushing to NuGet.org, consider:
- Adding a license file
- Adding a readme file
- Adding an icon (look for CC0, if publishing)
- Treat like a professional project
- Publish instructions on how to use
- Have unit tests
- Have continuous integration
- Track issues and put on project board
- Lowest version of .NET possible (use .NET Standard, if possible)
Create NuGet package from Class Library project
- View Class Library’s project properties
- Select “Package” section
- Check “Generate NuGet package on build”
- Package ID
- Naming rules
- Prefix, to make unique
- If posting to NuGet.org, maybe use your name or company name
- If internal, company or division name
- Package version
- Use good hygiene here
- Description, Copyright, Project URL, Icon, README, Repository URL, etc.
- Nice to have if publishing to NuGet.org
- To build, right-click on project and select “Pack”
- Creates .nupkg file in project’s \bin\release folder
- The one file is all you need to copy
- Ignore the sub-directory
- File name includes version number
- This is a zip file
- The one file is all you need to copy
- Creates .nupkg file in project’s \bin\release folder
- Select “Package” section
Make your NuGet package available to others
- For personal/internal packages, copy to directory on the network
- For public projects, upload to nuget.org
Using a local NuGet package
- Manage NuGet packages for solution
- Add new package source
- Network share for your corporate packages
- FYI: Visual Studio Offline Packages is a local cache of your used packages
- Add new package source
- Select “Package source” that includes the package location
- Default is “nuget.org”