How to develop a NuGet Package?

Enis Necipoğlu
4 min readOct 15, 2020

--

Each .Net Developer knows nuget.org and NuGet Packages. So, have you ever been though about how to create a nuget package? Nuget packages are easy to plug-in libraries. So a nuget package can be added to any project via using NuGet Package Manager.

Anatomy of a Nuget Package

Nuget packages aren’t just dll files. A nuget package is portable and includes any content which you want to place into a .Net Project. You can place a txt file or a png file in it. That’s why we call them as ‘Package’. You can pack some of development files and move them project to project with .nupkg format.

I’ve just extracted my InputKit nuget package, you can see tree-view of an nupkg file in the following image.

Xamarin.Forms.InputKit.3.4.3.nupkg

So just review folders and files in it.

icon.png: This is icon file of your NuGet package. It’s embeded in .nupkg now, but it could be defined only as url in earlier version of NuGet.

.nuspec: Nuspec is a metadata file about your nuget package. In earlier version, it used to use to pack your nuget package. But in new dotnet CLI this file is generated from your .csproj file by roslyn compiler. It includes your package’s target frameworks, names, license, icon, tags, dependencies to other packages and static files described in it to place to added project.

[Content_Types].xml: This is a metadata file which provides each file extensions included in package.

lib: Main folder of your package. This folder includes your build output. In another say, it includes your bin folder after build. So you can see different target framework’s folders as same as your project output. For example, if your .csproj file is multi-targeted, you can see a folder for each of your target framework like as mine.

package: This folder includes more metadata files about your package. This data is same data as what you see in nuget packages list: ‘creator’, ‘description’, ‘identifier’, ‘version’ and‘keywords’.

_rels/.rels: This is an xml formatted file extension which created and used by microsoft. You can see more information about .rels format from here. It’s used for Microsoft Office mostly.

Create your first Library

Each .Net Developer knows about class libraries. They are hard to move or use in different project. Because their outputs are dll files. I won’t talk about how to build a library in this article. I’ll just show how to convert them into a portable nuget packages. So let’s start with first step.

1- Choosing Target Framework

Choosing target framework is so important! Just plan your project and define your dependencies. If your dependencies require .Net Core like Entity Framework or something which targets .Net Core, just pick your target framework as ‘.Net Core’. But if not, try always to build your library on ‘.Net Standard’. By the way, you can build a multi-targeting project. You can read more about multi-targeting projects from here.

2-Fill the MetaData

MetaData is most important thing in the age which we live in. MetaData makes easier to find your package and describes best what it does. So please fill your metadata correctly. After you created you Class Library in Visual Studio, just Right Click and go Properties. You can see all fields which you can fill under Package tab.

If your environment isn’t Visual Studio, you can modify directly your .csproj file like below.

3- PACK IT!

Here is the where magic happen! Prepare your code, Right Click your project and click the Pack button. Your nupkg file will be waiting for you in your project’s bin folder.

If your development environment isn’t Visual Studio, you can do it with command prompt like following:

dotnet pack My.Package.csproj

4- Share with whole World!

Go nuget.org and log in to your account. Then navigate to Publish tab and drop your .nupkg file into that page. Fill information fields about your package and submit it. That’s it! This is the easiest way to publish your package on nuget.org.

With command prompt

But you can do it with command prompt too. Let’s see how it can be in this way.

Firsly you need an Api-Key to communicate with nuget API. Go to your profile on nuget.org and find API Keys section like below.

Create an API-Key and keep that. You can not be able to see it again. So you need to keep it.

Now, start with packing then you can push via CLI.

dotnet pack --configuration Release -o .packages/

That means build project in Release mode, and place output files into ‘.packages’ folder. So we can find easily our output from .pacakges folder. So your nupkg file is ready to push. Use following command to push your package to nuget.org or your custom defined source:

dotnet nuget push .\.packages\My.Pacakge.1.0.1.nupkg --source https://api.nuget.org/v3/index.json --api-key YOUR_API_KEY --skip-duplicate

I used skip-duplicate argument, because if you’re configuring a CI, same version of package can be pushed multiple times and if you’re pushing already existing version, you’ll get an error and CI-CD process will be failed. So using skip-duplicate argument, ignores already existing version pushes and completes operation successful.

Thank you for reading. See you at next article 👋

--

--

No responses yet