Consume a .NET Standard library in Visual Studio 2017

Once you've created a .NET Standard class library by following the steps in Building a C# class library with .NET Core in Visual Studio 2017 or Building a Visual Basic class library with .NET Core in Visual Studio 2017, tested it in Testing a class library with .NET Core in Visual Studio 2017, and built a Release version of the library, the next step is to make it available to callers. You can do this in two ways:

  • If the library will be used by a single solution (for example, if it's a component in a single large application), you can include it as a project in your solution.

  • If the library will be generally accessible, you can distribute it as a NuGet package.

Including a library as a project in a solution

Just as you included unit tests in the same solution as your class library, you can include your application as part of that solution. For example, you can use your class library in a console application that prompts the user to enter a string and reports whether its first character is uppercase:

  1. Open the ClassLibraryProjects solution you created in the Building a C# Class Library with .NET Core in Visual Studio 2017 topic. In Solution Explorer, right-click the ClassLibraryProjects solution and select Add > New Project from the context menu.

  2. In the Add New Project dialog, expand the Visual C# node and select the .NET Core node followed by the Console App (.NET Core) project template. In the Name text box, type "ShowCase", and select the OK button.

    Visual Studio Add New Project dialog - C#

  3. In Solution Explorer, right-click the ShowCase project and select Set as StartUp Project in the context menu.

    Visual Studio project context menu to set startup project- C#

  4. Initially, your project doesn't have access to your class library. To allow it to call methods in your class library, you create a reference to the class library. In Solution Explorer, right-click the ShowCase project's Dependencies node and select Add Reference.

    Visual Studio project add reference context menu - C#

  5. In the Reference Manager dialog, select StringLibrary, your class library project, and select the OK button.

    Visual Studio Manage references dialog - C#

  6. In the code window for the Program.cs file, replace all of the code with the following code:

    using System;
    using UtilityLibraries;
    
    class Program
    {
        static void Main(string[] args)
        {
            int row = 0; 
    
            do
            {
                if (row == 0 || row >= 25)
                   ResetConsole();
    
                string input = Console.ReadLine();
                if (String.IsNullOrEmpty(input)) break;
                Console.WriteLine($"Input: {input} {"Begins with uppercase? ",30}: " +
                                  $"{(input.StartsWithUpper() ? "Yes" : "No")}\n");
                row += 3;                  
            } while (true);
            return;
    
            // Declare a ResetConsole local method
            void ResetConsole()
            {
                if (row > 0) {
                   Console.WriteLine("Press any key to continue...");
                   Console.ReadKey();
                }   
                Console.Clear();
                Console.WriteLine("\nPress <Enter> only to exit; otherwise, enter a string and press <Enter>:\n");
                row = 3;  
            }
        }
    }
    
    

    The code uses the row variable to maintain a count of the number of rows of data written to the console window. Whenever it is greater than or equal to 25, the code clears the console window and displays a message to the user.

    The program prompts the user to enter a string. It indicates whether the string starts with an uppercase character. If the user presses the Enter key without entering a string, the application terminates, and the console window closes.

  7. If necessary, change the toolbar to compile the Debug release of the ShowCase project. Compile and run the program by selecting the green arrow on the ShowCase button.

    Visual Studio project toolbar showing Debug button - C#

You can debug and publish the application that uses this library by following the steps in Debugging your Hello World application with Visual Studio 2017 and Publishing your Hello World Application with Visual Studio 2017.

Distributing the library in a NuGet package

You can make your class library widely available by publishing it as a NuGet package. Visual Studio does not support the creation of NuGet packages. To create one, you use the dotnet command line utility:

  1. Open a console window. For example in the Ask me anything text box in the Windows taskbar, enter Command Prompt (or cmd for short), and open a console window by either selecting the Command Prompt desktop app or pressing Enter if it's selected in the search results.

  2. Navigate to your library's project directory. Unless you've reconfigured the typical file location, it's in the Documents\Visual Studio 2017\Projects\ClassLibraryProjects\StringLibrary directory. The directory contains your source code and a project file, StringLibrary.csproj.

  3. Issue the command dotnet pack --no-build. The dotnet utility generates a package with a .nupkg extension.

    Tip

    If the directory that contains dotnet.exe is not in your PATH, you can find its location by entering where dotnet.exe in the console window.

For more information on creating NuGet packages, see How to Create a NuGet Package with Cross Platform Tools.