Creating a Net Framework package
Creating a NuGet package from a .NET Framework Class Library involves creating the DLL in Visual Studio on Windows, then using the nuget.exe command line tool to create and publish the package.
Download nuget.exe
Install the nuget.exe
CLI by downloading it from nuget.org, saving that .exe
file to a suitable folder, and adding that folder to your PATH environment variable.
Create a class library project
You can use an existing .NET Framework Class Library project for the code you want to package, or create a simple one as follows.
In Visual Studio, choose File > New > Project, select the Visual C# node, select the "Class Library (.NET Framework)" template, name the project AppLogger, and click OK.
Configure project properties for the package
A NuGet package contains a manifest (a .nuspec
file), that contains relevant metadata such as the package identifier, version number, description, and more. Some of these can be drawn from the project properties directly, which avoids having to separately update them in both the project and the manifest. This section describes where to set the applicable properties.
1. Select the Project > Properties menu command, then select the Application tab.
2. In the Assembly name field, give your package a unique identifier.
3. Select the Assembly Information... button, which brings up a dialog box in which you can enter other properties that carry into the manifest. The most commonly used fields are Title, Description, Company, Copyright, and Assembly version. These properties ultimately appear with your package on a host like nuget.org, so make sure they're fully descriptive.
4. Optional: to see and edit the properties directly, open the Properties/AssemblyInfo.cs
file in the project.
5. When the properties are set, set the project configuration to Release and rebuild the project to generate the updated DLL.
Generate the initial manifest
With a DLL in hand and project properties set, you now use the nuget spec
command to generate an initial .nuspec
file from the project. This step includes the relevant replacement tokens to draw information from the project file.
You run nuget spec
only once to generate the initial manifest. When updating the package, you either change values in your project or edit the manifest directly.
1. Open a command prompt and navigate to the project folder containing AppLogger.csproj
file.
2. Run the following command: nuget spec AppLogger.csproj
. By specifying a project, NuGet creates a manifest that matches the name of the project, in this case AppLogger.nuspec
. It also include replacement tokens in the manifest.
3. Open AppLogger.nuspec
in a text editor to examine its contents, which should appear as follows:
<?xml version="1.0"?>
<package >
<metadata>
<id>Package</id>
<version>1.0.0</version>
<authors>YourUsername</authors>
<owners>YourUsername</owners>
<license type="expression">MIT</license>
<projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
<iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package description</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2019</copyright>
<tags>Tag1 Tag2</tags>
</metadata>
</package>
Edit the manifest
1. NuGet produces an error if you try to create a package with default values in your .nuspec
file, so you must edit the following fields before proceeding. See .nuspec file reference - optional metadata elements for a description of how these are used.
- license
- projectUrl
- iconUrl
- releaseNotes
- tags
2. For packages built for public consumption, pay special attention to the Tags property, as tags help others find your package on sources like nuget.org and understand what it does.
3. You can also add any other elements to the manifest at this time, as described on .nuspec file reference.
4. Save the file before proceeding.
Run the pack command
1. From a command prompt in the folder containing your .nuspec
file, run the command nuget pack -Properties Configuration=Release
2. NuGet generates a .nupkg
file in the form of identifier-version.nupkg, which you'll find in the current folder.