Introduction
NuGet is the most useful and essential tool for modern software development platforms via which developers can create, share and consume reusable code. Those useful or reusable common codes are compiled as DLLs and published as packages with code-related files and other information, including the version number.
The nuget.org doesn't allow permanently deleting the already published packages because if you delete the package after a few days/hours of publishing it, others might have already used it, and their project can be broken. Because of this reason, NuGet does not allow for deletion. So before publishing your package, you must verify the package functionality properly. You must test it locally before publishing it in the package store (nuget.org). Additionally, sometimes organizations don't want to use online packages or restrict to use of online packages. They just download and store the valid packages locally or somewhere in the private cloud and then ask a developer to use them from the local or private cloud storage. For this scenario, this article guides you through configuring and utilizing the package.
This article demonstrates how to create a NuGet package using Visual Studio and test it locally before publishing it to nuget.org. In addition, we will learn to create a nupkg file and configure the NuGet manager for local testing in Visual Studio before publishing it to NuGet. Basically, this article focuses on how to configure and set up a test environment locally to test the package before publishing it. Testing and verifying depend upon the package you built; however, this article explains the technique and process to configure and test the package locally.
If you are new to NuGet package creation, a previous article on creating and publishing a NuGet package can be found here.
Prerequisites
Create or Update Class Library Project
Case I: New package
If you are creating the NuGet package for the first time, you can also test it before publishing it. First, complete the code for your class library project that you will publish in the package store. After that, you can follow the below steps to test it locally before publishing it to the NuGet store.
Case II: If updating new features or fixing issues of the existing package project.
Open your class library project and update the new changes for your project. Once updated, we need to test the package before publishing it.
Create a Local Feed and Publish the Package
Step 1. Create a local feed to store the NuGet
Firstly we will create a new folder in your drive, such as in C:\ drive. You can create a folder with the name: local.nuget. Here we will store the package locally.
Step 2. Configure Package Manager for Local feed
After that, Open Visual Studio and go to Tools-> Nuget Package Manager ->Package Manager Settings, as portrayed below.
Then go to NuGet Package Manager->Package Sources, as shown below. And click on the + green button. Provide the name and directory of your nuget folder just created before(local.nuget). Then click on OK.
Step 3. Provide Package Details
Once the program is ready, we will create and publish a package to a local or custom feed. In this step, let's provide package details.
Open the project that you want to publish as a package. Right-click on the project and then go to the property of the Project. Go to the package section. Then provide all the details such as package Id, name, version, tag name, description, and so on of your package as shown below.
Step 4. Create/Publish NuGet Pack file
Once you provide all the package details and your project is ready to create a package file, right-click on the project and then click on Pack, as illustrated below.
Then it will create a nupkg file in your bin folder.
Step 5. Add the package to the local NuGet Feed
Go to the bin folder and copy the yourprojectname.nupkg file, and paste it to the custom NuGet folder created earlier i.e., "C:\local.nuget"
Up to now, we have created a Package. It's time for us to the package and check it.
Use the Custom package and Test
This section describes how to configure and get the package from the local feed (local drive) to the Visual Studio project. Now, we will add the package from the local drive.
Step 1. Open the solution, which will use the package that we have created.
Open the package manager for this project. Right-click on the project and then go to Manage NuGet Package, as shown below.
Step 2. Go to the Settings icon, select your local Package store, change the package source to local.nuget as shown below, and click Ok. This will load the package from the local drive (C:/local.nuget) we created.
Step 3. Afterward, go to the Browse tab, where you can see your package like other normal NuGet packages. Select your package and install it.
Once installed, you are ready to test it. After verifying and testing, you can publish the same file to the NuGet store.
Summary
Hence, the article has described how to create a custom NuGet Package in Visual Studio and check it locally. We have also learned to create a custom NuGet directory and configure it to test the package. This is required for us when we need to test the new package or updated version of the package before publishing it to nuget.org.
Reference