Creating and Referencing a NuGet Package in Visual Studio

Follow the instructions below to package a .NET assembly with NuGet and reference that package in another assembly.

NuGetTestLibrary

1. Start Microsoft Visual Studio.

2. In Visual Studio, select the item “File – New – Project…” from the main menu bar.

3. In the New Project dialog that appears, select the “Blank Solution” template, enter the value “NuGetTest” in the Name box, enter any convenient directory in the Location box, and click the OK button. A new solution named “NuGetTest” will be created, the dialog will disappear, and focus will return to the main Visual Studio window, where a node for the NuGetTest solution will be present in the Solution Explorer pane.

4. In the Solution Explorer pane, right-click the name of the “NuGetTest” solution’s node and select the item “Add – New Project…” from the context menu that appears. The “Add New Project” dialog will appear.

5. In the Add New Project dialog, select the “Visual C# – Console Application” template, enter the value “NuGetTestApplication” in the Name box, and click the OK button. The dialog will disappear, a new project named “NuGetTestApplication” will be created, and focus will return to the main VisualStudio window. A new node for the NuGetTestApplication project will be present in the Solution Explorer pane, along with several subnodes. The file “Program.cs” will be opened in the text editor pane.

6. Replace the default contents of the file Program.cs with the following text and save.


using System;

namespace NuGetTestApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            NuGetTestLibrary.Class1.SayHelloNuGet();
            Console.Write("Press any key to quit.");
            Console.ReadKey();
        }
    }
}

7. In the Solution Explorer pane, right-click the NuGetTest solution node again and select the item “Add – New Project…” from the context menu that appears. The Add New Project dialog will appear again.

8. In the Add New Project dialog, select the “Visual C# – Class Library” template, enter the value “NuGetTestLibrary” in the Name box, and click the OK button. The dialog will disappear, a new project named “NuGetTestLibrary” will be created, and focus will return to the main VisualStudio window. A new node for the NuGetTestLibrary project will be present in the Solution Explorer pane, along with several subnodes. The file “Class1.cs” will be opened in the text editor pane.

9. Replace the default contents of the file Class1.cs with the following text and save.


using System;

namespace NuGetTestLibrary
{
    public class Class1
    {
        public static void SayHelloNuGet()
        {
            Console.WriteLine("Hello, NuGet!");
        }
    }
}

10. In the Solution Explorer pane, locate the “References” node under the “NuGetTestApplication” project’s node. Right-click the “References” node, and select the “Add Reference” item from the context menu that appears. The “Reference Manager” dialog will appear.

11. In the Reference Manager dialog, activate the checkbox next to the “NuGetTestLibrary” list item, then click the OK button. The dialog will be dismissed, focus will return to the main Visual Studio screen, and a new reference will be added in the Solution Explorer pane.

12. In the main menu bar, change the value in the build mode dropdown from “Debug” to “Release” if necessary.

13. Compile and run the solution by pressing Ctrl-F5. Verify that the message “Hello, NuGet!” appears in the console, then press a key to exit the console.

14. If you have not already done so, download the command-line tool NuGet.exe from nuget.org. As of this writing, the latest version is available for download at the URL “https://www.nuget.org/nuget.exe”. Copy the downloaded file into the NuGetTest directory.

15. Open a command prompt window and navigate to the “NuGet/NuGetTestLibrary/bin/Release” directory.

16. Enter the commands below to create a new directory named “lib” and move the NuGetTestLibrary.dll file into it.

mkdir lib
move NuGetTestLibrary.dll lib

16. At the command prompt, enter the command “..\..\..\nuget.exe spec NuGetTestLibrary.dll” to generate the file “NuGetTestLibrary.dll.nuspec”.

17. If desired, open the file NuGetTestLibrary.dll.nuspec in a text editor and substitute the desired values in the “tags”, “summary”, and “description” elements. This step is optional, but it will reduce the number of warnings displayed by running “nuget pack”.

16. Still at the command prompt, enter the command “..\..\..\nuget.exe pack NuGetTestLibrary.dll.nuspec” to package the assembly into the file “NuGetTestLibrary.dll.1.0.0.nupkg”.

17. Copy the newly created NuGetTestLibrary.dll.1.0.0.nupkg package to the top NuGetTest directory.

19. From the main menu, select the item “Tools – NuGet Package Manager – Package Manager Settings”. On the Options dialog that appears, click the “NuGet Package Manager – Package Sources” node in the tree view. Click the “Add” button at the upper-right corner of the dialog to create a new package source entry in the list. Enter the value “NuGetTest” in the Name box, enter the path to the NuGetTest directory in the Source box, click the Update button to confirm the changes, and then click the OK button to dismiss the dialog.

20. Back in Visual Studio, in the Solution Explorer, locate the node “NuGetTest – NuGetTestApplication – References – NuGetTestLibrary”, right-click it, and select the item “Remove” from the context menu that appears.

21. Attempt to build the solution by pressing Ctrl-Shift-B. Because the reference to the NuGetTestLibrary project was removed in the previous step, an error will be displayed stating that “The name ‘NuGetTestLibrary’ does not exist in the current context”, and the build will not succeed.

22. In the Solution Explorer pane, right-click the References node and select the item “Manage NuGet Packages” from the context menu that appears.
In the dialog that appears in the Editor pane, change the value of the Package Source dropdown to “NuGetTestLocal”, then click the item “NuGetTestLibrary.dll”
in the list to select it, click the Install button that appears to install the new library, and click the OK button on the confirmation dialog. A reference to the new NuGetTestLibrary will appear under the References node in the Solution Explorer.

23. Press Ctrl-F5 to re-build and run the solution. This time the build will succeed, because the NuGetTestApplication project has a valid reference to the newly created NuGetTestLibrary.dll package.

This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s