| title | Consuming a class library with .NET Core in Visual Studio 2017 | ||
|---|---|---|---|
| description | Learn how to call the members in a class library with Visual Studio 2017. | ||
| author | BillWagner | ||
| ms.author | wiwagn | ||
| ms.date | 08/07/2017 | ||
| ms.topic | article | ||
| ms.prod | .net-core | ||
| dev_langs |
|
||
| ms.workload |
|
Once you've created a 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.
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:
-
Open the
ClassLibraryProjectssolution 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. -
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.
-
In Solution Explorer, right-click the ShowCase project and select Set as StartUp Project in the context menu.
-
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
ShowCaseproject's Dependencies node and select Add Reference. -
In the Reference Manager dialog, select StringLibrary, your class library project, and select the OK button.
-
In the code window for the Program.cs file, replace all of the code with the following code:
[!CODE-csharpUsingClassLib#1]
The code uses the Console.WindowHeight property to determine the number of rows in the console window. Whenever the Console.CursorTop property is greater than or equal to the number of rows in the console window, 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.
-
If necessary, change the toolbar to compile the Debug release of the
ShowCaseproject. Compile and run the program by selecting the green arrow on the ShowCase button.
-
Open the
ClassLibraryProjectssolution you created in the Building a class Library with Visual Basic and .NET Core in Visual Studio 2017 topic. In Solution Explorer, right-click the ClassLibraryProjects solution and select Add > New Project from the context menu. -
In the Add New Project dialog, expand the Visual Basic 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.
-
In Solution Explorer, right-click the ShowCase project and select Set as StartUp Project in the context menu.
-
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
ShowCaseproject's Dependencies node and select Add Reference. -
In the Reference Manager dialog, select StringLibrary, your class library project, and select the OK button.
-
In the code window for the Program.vb file, replace all of the code with the following code:
[!CODE-vbUsingClassLib#1]
The code uses the Console.WindowHeight property to determine the number of rows in the console window. Whenever the Console.CursorTop property is greater than or equal to the number of rows in the console window, 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.
-
If necessary, change the toolbar to compile the Debug release of the
ShowCaseproject. Compile and run the program by selecting the green arrow on the ShowCase button.
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.
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:
-
Open a console window. For example in the Ask me anything text box in the Windows taskbar, enter
Command Prompt(orcmdfor 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. -
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.
-
Issue the command
dotnet pack --no-build. Thedotnetutility 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.exein the console window.
For more information on creating NuGet packages, see How to Create a NuGet Package with Cross Platform Tools.





