A senior web developer offers a quick tutorial for those new the NuGet package system on how to get up and running with NuGet packages using Visual Studio. Visual Studio; Step 1: Create a Project.
Let’s create another client project using Visual Studio. Right click on the project, and go to Manage NuGet Packages for Solution Click the Settings button (little gear icon) next to the. . Install the packages for the appropriate projects with Visual Studio (right-click solution, Manage NuGet packages for solution) After the packages are installed, Visual Studio will add appropriate directives to the.vcxproj files that reference the.props and.targets files from your NuGet package, while allowing the projects to load if. Create a class library project. You can use an existing.NET Standard Class Library project for the code you want to package or create a simple one as follows: Step 1. In Visual Studio, choose File New Project, expand the Visual C#.NET Standard node, select the 'Class Library (.NET Standard)' template, name the project ownNuGet,. That's why it can be interesting to use a nuget package like NuPack to do the job. NuPack is a nuget package for.NET 4.0+ that adds an additional build step to pack output of project into a nuget package. It is simple to add, use or remove because it is materialized by a single nuget package without any additional task to do.
NuGet packages a hugely important part of the .NET ecosystem. Learn how to get NuGet packages installed and running on your machine.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
NuGet packages are pre-defined pieces of code which are reusable for other developers. You can add a NuGet package to your project and use it's functionality wherever you want in your project. We can also say that NuGet behaves like a platform, where developers can create and share code with the world. It contains code that's compiled as DLL which is nearly impossible to decrypt.
Prerequisites
- Visual Studio
Step 1: Create a Project
- We can install NuGet packages in any .NET project which supports the same target framework as our project.
- For this demo, I'm using a standard MVC Project.
Step 2: Open the NuGet Package Manager
Now, there are two options for opening the Nuget Package Manager window.
Select Tools > Nuget Package Manager > Manage Nuget Packages for Solution.
2. Go to your Solution Explorer, right-click on References, and choose Manage NuGet Packages.
Step 3: Search and Install Your Required NuGet Package
- In this window, you can search for, install, uninstall and update any Nuget Package. You can also see a full list of your installed NuGet packages.
- Once you click on the Install button and click on 'I Agree' in the permissions pop-up window, then that particular package will be installed/added to your project.
- Here, I am installing the EntityFramwork Nuget Package.
Once the package is installed successfully, this message will be displayed.
Here, You can see your installed project. Go to Solution Explorer > Reference, and expand it.
Now you can access that package/code from your project.
Note: You can also install NuGet packages for multiple projects at the same time and in the same place.
Installing NuGet Using the Package Manager Console
Step 1: Select Tools > NuGet Package Manager > Package Manager Console
It will open the console at the bottom of the window.
Step 2: Select Your Project and Execute the Below Command to Install the Package
If your command is valid, then the package will be installed in your project.
Opinions expressed by DZone contributors are their own.
Popular on DZone
NuGet packages contain reusable code that other developers make available to you for use in your projects. See What is NuGet? for background. Packages are installed into a Visual Studio project using the NuGet Package Manager, the Package Manager Console, or the dotnet CLI. This article demonstrates the process using the popular Newtonsoft.Json package and a Windows Presentation Foundation (WPF) project. The same process applies to any other .NET or .NET Core project.
Once installed, refer to the package in code with using <namespace>
where <namespace> is specific to the package you're using. Once the reference is made, you can call the package through its API.
Tip
Start with nuget.org: Browsing nuget.org is how .NET developers typically find components they can reuse in their own applications. You can search nuget.org directly or find and install packages within Visual Studio as shown in this article. For general information, see Find and evaluate NuGet packages.
Prerequisites
- Visual Studio 2019 with the .NET Desktop Development workload.
You can install the 2019 Community edition for free from visualstudio.com or use the Professional or Enterprise editions.
If you're using Visual Studio for Mac, see Install and use a package in Visual Studio for Mac.
Create a project
NuGet packages can be installed into any .NET project, provided that the package supports the same target framework as the project.
For this walkthrough, use a simple WPF app. Create a project in Visual Studio using File > New Project, typing .NET in the search box, and then selecting the WPF App (.NET Framework). Click Next. Accept the default values for Framework when prompted.
Visual Studio creates the project, which opens in Solution Explorer.
Add the Newtonsoft.Json NuGet package
To install the package, you can use either the NuGet Package Manager or the Package Manager Console. When you install a package, NuGet records the dependency in either your project file or a packages.config
file (depending on the project format). For more information, see Package consumption overview and workflow.
NuGet Package Manager
In Solution Explorer, right-click References and choose Manage NuGet Packages.
Choose 'nuget.org' as the Package source, select the Browse tab, search for Newtonsoft.Json, select that package in the list, and select Install:
If you want more information on the NuGet Package Manager, see Install and manage packages using Visual Studio.
Accept any license prompts.
(Visual Studio 2017 only) If prompted to select a package management format, select PackageReference in project file:
If prompted to review changes, select OK.
Package Manager Console
Select the Tools > NuGet Package Manager > Package Manager Console menu command.
Once the console opens, check that the Default project drop-down list shows the project into which you want to install the package. If you have a single project in the solution, it is already selected.
Enter the command
Install-Package Newtonsoft.Json
(see Install-Package). The console window shows output for the command. Errors typically indicate that the package isn't compatible with the project's target framework.If you want more information on the Package Manager Console, see Install and manage packages using Package Manager Console.
Use the Newtonsoft.Json API in the app
Create Nuget Package Visual Studio
With the Newtonsoft.Json package in the project, you can call its JsonConvert.SerializeObject
method to convert an object to a human-readable string.
Open
MainWindow.xaml
and replace the existingGrid
element with the following:Open the
MainWindow.xaml.cs
file (located in Solution Explorer under theMainWindow.xaml
node), and insert the following code inside theMainWindow
class:Even though you added the Newtonsoft.Json package to the project, red squiggles appears under
JsonConvert
because you need ausing
statement at the top of the code file:Build and run the app by pressing F5 or selecting Debug > Start Debugging:
Select on the button to see the contents of the TextBlock replaced with some JSON text:
Related video
Nuget Push Command
Find more NuGet videos on Channel 9 and YouTube.
Create Nuget Package Visual Studio 2019
Next steps
Create Nuget Package Visual Studio 9
Congratulations on installing and using your first NuGet package!
Create Nuget Package Command Line
To explore more that NuGet has to offer, select the links below.