Creating NuGet package using Visual Studio 2019 and publishing it to Nuget.org
In this post, we will understand how can we create a NuGet package using Visual Studio 2019. Also, we will understand, how to deploy this package to Nuget.org to make it availab...
Ankush Jain
Lead Software Engineer (VP) | Software Architect
In this post, we will understand how can we create a NuGet package using Visual Studio 2019. Also, we will understand, how to deploy this package to Nuget.org to make it available to the rest of the world.
Step 1: Open Visual Studio 2019 and create a Class Library (.NET Standard) project.
Note: We should choose .NET Standard for creating Class Library projects. The reason being, libraries created using .NET Standard can be used by all major platforms such as .NET Framework, .NET Core, Mono, Xamarin or Unity.
Step 2: Provide a name for your project. My project name is CoderJony.Utilities.Logger. Try to keep this project name the same as your NuGet package name just for consistency.
Step 3: Now I create a public Logger class and define some public methods inside that class.
Step 4: Go to the properties of the project and select the package tab.
Step 5: This is the most useful step. In this step, I will tell you about the properties that you need to take care of the most.
- Package id - This should be the unique name for your NuGet package. This should not conflict with any other package available on the NuGet server.
- Package version - This is the version displayed on NuGet.org for your NuGet package.
- Authors - This could be your company name or your name if you are an individual.
- Company - This should be your company name.
- Product - This is the product name. You can keep it the same as the package id.
- Description - This should be the description of your package.
- Licensing - This field is required. Either you have to provide some expression here OR you can also upload a local licensing file here. I am using MIT as a license expression. Click here for more information about License Expressions.
- Repository Url - This is optional, but you can provide your public repository URL here such as GitHub.
- Tags - You should provide comma-separated values here. This helps in indexing your package on NuGet. So that NuGet can show your package as a search result when someone searches for it.
- Assembly Version & Assembly File Version - This should be the Assembly Version of the project. But it is advised to keep it the same as Package Version to avoid confusion at a later stage.
The more information you provide here, the more informative your package will look in the search results.
Step 6 - Below, you can see, that I have provided all the information that is required for the NuGet package.
Step 7 - Just mark these two checkboxes that are highlighted below.
- Generate NuGet package on build - This will generate a NuGet package every time you build the project.
- Require license acceptance - This will ensure that users have to accept the license while installing the package.
Step 8 - Now select Release mode and build the application. You can see a .nupkg file has been generated inside ~\bin\release\ folder.
Step 9 - Here you can see the generated .nupkg file.
Step 10 - Now login to nuget.org with your Microsoft account and click on Upload Package link.
Step11 - Drag and drop the generated .nupkg file here. As you can see I have dropped my .nupkg file here.
Step 12 - Verify the provided information and press the submit button at the end of the page. After submitting the information, you will be redirected to your package's page.
Note: NuGet takes some time to validate your package after upload. After around 5 minutes, you will find that this warning is removed automatically.
Step 13 - After a few minutes, you can see - your package is available for use.
Step 14 - Now we can install this NuGet package in any of our .NET projects.
Congratulations !!! We have created a NuGet package in less than 30 minutes. Please let me know your feedback and suggestions in below comment box.
Happy Coding :)