NuGet is one of the most powerful tools through which you can package and ship your code to different teams and resources across multiple organizations. These packages can be downloaded through visual studio through package manager console or “Manage NuGet Packages.” There are several NuGet packages available in NuGet.org. It is an open-source platform that has access to thousands of packaged libraries that can be used by people on the internet.
You can create and install a NuGet package from your local computer, GitHub, or NuGet. If your package is utilized internally by your development team, you can upload this package in your internal dev ops repository. If you want to contribute this as an open-source library, you can upload this library on NuGet to help the development community.
For example, there is a Power Apps PCF controls library available on NuGet for power apps developers to utilize this package for PCF development. In this blog article, we will walk you through the process of creating your first NuGet package and show you how you can deploy it using Visual Studio.
Now, let us go ahead and see how we can create and publish a NuGet package.
Create a NuGet package:
- Create a new visual project, add a class file
- Build the project. This will generate a dll file in the bin folder
- Download the latest NuGet.exe file from nuget.org. The latest version is recommended
- Put this exe file in the project root folder that we built in step#2
- For example, if your project name is ConsoleApplicaton1 and your local folder is C:/user/your user/ ApplicationInsightsLogKeyVault-> Place the NuGet.exe file in this folder
- Open command prompt (Ctr+R) -> type cmd. In the command prompt, navigate to the folder where you have the NuGet.exe file. For example, cd C:/user/your user/ ApplicationInsightsLogKeyVault
- Type the following command to create nuget spec file nuget spec
- This will create ApplicationInsightsLogKeyVault.nuspec file in the root folder of the project
The .nuspec file contains configuration xml elements of your NuGet package. You can specify the details of your package such as ID, Version number, Authors, Owners, Project URL, Icon URL, Description, Release Notes, etc.
A .nuspec file will look like this:
The dependencies section is important. The dependencies tell the NuGet package to look for all of the dependency packages and install them. Additionally, in the file sections, you can add the files to your project. The src field indicates the file that needs to copy, and the target path indicates the location this file needs to go in your project folder. This content\MyClass.cs will add a file to your root folder. If you want to add this file to a specific folder, you may specify the folder name Example - content\App\MyCalss.cs. This will create a new folder “App” in your target project and add the file “MyClass.cs” under that folder.
Similarly, for a class library file, you must specify the source as the class library, and target the “lib” folder.
For example, if your dll file is in bin/Debug/ folder, your src will look like this:
<file src="ConsoleApp1\bin\Debug\MyClass.dll" target="lib"/>
Publish the NuGet package:
Run the following command on the command line:
nuget pack ConsoleApp1.nuspec
This will create a ConsoleApp126.96.36.199.nupkg.
To publish this package from your local machine, go to Visual studio target project -> Tools -> Options -> Add.
Browse to the local folder where you have the nupkg file.
You will now see this package in the Visual studio.
This will allow you to browse to the package. Install the package from here.
Publish your NuGet package on NuGet.org
- Go to nuget.org.
- Sign in if you have an account. Register if you don’t have one already.
- Go to the upload tab.
- Upload your .nupkg file.
- Click Upload.
Your package is now published and live on NuGet.
I hope that this blog has helped you understand how you can easily publish a NuGet package. If you have any questions or insights on the blog, please leave a comment below!