Using NuGet to publish .NET packages
NuGet is a free and open-source package manager for the .NET ecosystem. We can create and install packages using NuGet client tools. All of the .NET packages are hosted for publishing and consumption on a central package repository known as NuGet Gallery.
- Visual Studio 2017 (with .NET-related workload)
- nuget.exe (add it’s location to PATH environment variable)
- Valid account on nuget.org
Create a class library project
For a .NET package to be published in the NuGet Gallery, it should be a valid class library project. The following instructions can be used to create a simple class library project:
- Open Visual Studio, go to File > New > Project, expand the Visual C# > .NET Standard node. Select the “Class Library (.NET Standard)” template and provide a valid name.
- To build the project, right-click on the project file and select Build. A DLL file will be generated in the Debug folder or Release folder (if you build the configuration)
For a real useful NuGet package, you should write necessary code which can be used by others to develop applications. However, a class library from the template is sufficient to create a package.
Configure Package Properties
- Go to Project > Properties, select Package tab.
- Provide a unique identifier for your package and fill out other required properties. For a description of various properties, please visit here. The properties provided at this stage will be defined in .nuspec manifest that is created by Visual Studio for the project.
- To view the properties directly in the project file, right-click the project in Solution Explorer and select Edit AppLogger.csproj.
Run the pack command
- Set the configuration to Release.
- Right click the project in Solution Explorer and select the Pack command.
- Visual Studio builds the project and creates the .nupkg file. Please note that the built package is in bin\Release\netstandard2.0 as befits the .NET Standard 2.0 target.
Acquire API Key
- Sign in to your nuget.org account or create an account if it doesn’t already exist.
- Select your user name on the top right, then select API Keys.
- Select Create, provide a name for your key, select Select Scopes > Push.
- Under API Key, enter * for Glob pattern, then select Create.
- After the key is created, select Copy to retrieve the access key needed for publishing the package.
Important: Save your key in a secure location because you cannot copy the key again later on. If you return to the API key page, you need to regenerate the key to copy it.
Publish with nuget push
- Open Command Prompt.
- Change to the folder containing the .nupkg file.
- Run the following command, specifying your package name and replacing the key value with your API key:
nuget push &lt;PACKAGE-NAME&gt;.nupkg &lt;API-KEY&gt; -Source https://api.nuget.org/v3/index.json
- nuget.exe displays the results of the publishing process.
Manage the published package
You can view your published package in your profile on nuget.org. Select Manage Packages to see the one that was just published. It might take a while for your package to be visible in search results.
If you want to unlist the package and hide it from search results, follow the steps listed below:
- On nuget.org, select your user name on top right, then select Manage Packages.
- Find the package to be unlisted under Published and select the trash can icon on the right.
- On the next page, clear the box labeled List (package-name) in search results and select Save.