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.
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 then 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 have to verify the package functionality properly. It is required for you to test it locally before publishing it in the package store (nuget.org)
This article demonstrates how we can 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 that you have built; however, this article explains the technique and process to configure and test the package locally.
If you are new to NuGet package creation, then a previous article on how to create and publish NuGet package can be found here.
Prerequisites
- Visual Studio 2022
Create or Update Class Library Project
Case I: New package
If you are creating the NuGet package for the first time also you can test the package before publishing it. For that, firstly 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 package locally and configure the setting for it.
Create a new folder in your drive such as in C:\ drive you can create a folder with the name: local.nuget.
Step 2- After that, Open Visual Studio and then 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 + 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
Now we will create a package and publish it to a local or custom feed.
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 then 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 as shown below and select your local Package store and change the package source to local.nuget as shown below and click on Ok. This will load the package from the local drive (C:/local.nuget) that we have created and kept the packed file there.
Step 3- After that, go to the Browse tab and there you can see your package as 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. Additionally, we have 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.