Guide For Building C# Apps On Ubuntu: NuGet Packages

Introduction and Background

If you have been programming C# applications, then chances are you are pretty much aware of what NuGet packages are. However, if you don’t know what that is, it is an online storage for dynamic-link libraries (.dll) for .NET programs. Most programmers (including me) have contributed toward the total packages currently available on NuGet. NuGet allows developers to reference the libraries from third-party programmers, easily, all by typing a command!

In the previous posts, I had already talked about the basics of Ubuntu programming using Mono, in this short piece I am going to talk about NuGet packages in Mono. NuGet packages are of great use in Visual Studio, you can download the most wanted packages from NuGet to increase the productivity of your application. I have personally used Newtonsoft. Json package many times to introduce data sources in my applications based on JSON files and C# code, a usual example of this can be LoginKeys framework that I wrote for authentication systems. There are many other useful packages that you will like to use while programming, in this post I am going to talk about methods to include the package in your application development project.

Managing the NuGet packages

In this section, I will demonstrate two things:

  1. How to manage the repositories, from where to fetch from.
  2. How to install the packages and use them.

This will help you have a good understanding of NuGet packages, package management, and how to use them in your projects.

Configuring the IDE

First step is to configure the IDE, by default, IDE is configured to capture the packages from NuGet’s current API version, which is, https://www.nuget.org/api/v2/. In future that may change, so that is why you may need to configure the IDE, Just showing, the settings are available under IDE settings.

NuGet package gallery management
Figure 1: NuGet package gallery management

You can edit this to configure the IDE to capture the packages from the actual location. You can also open these settings from the NuGet package management window itself, by selecting “Configure sources”.

Options available for currently provided NuGet g
Figure 2:
Options available for currently provided NuGet galleries and option to configure the sources

This will allow you to alter the sources and other settings, you can also change the names for the galleries. However, that is pretty simple, and I am sure you can manage to do that all by yourself. Now, let us consider installing and using the packages in our applications.

Installing the packages

Fun part starts! The packages can be easily installed using the GUI window provided to us by MonoDevelop itself. We can search for the packages, or install the most popular ones that are shown in the list view itself. It is very simple to install the package.

I have demonstrated the procedure in the following image:

NuGet package management window
Figure 3: NuGet package management window

Now, what I have done is that I have introduced steps required to install the package. You can follow the same steps to install any package.

  1. Find the package in the list. I have selected, Json.NET package.

  2. If you find yourself confused while selecting a package, you can check for the explanation of the package in the right side, name, version, description and author details are shared here. You can make sure you are selecting the “correct” version of library.

  3. Click on “Add Package” to add it.

  4. This option is not required, and is used to turn on and off beta versions, pre-releases.

Now, once you have clicked on the “Add Package” button, MonoDevelop will take a minute or two and then add the library package to your application. In my system, it took ~5 seconds, so don’t worry, it will download and install the packages, in a while. Once that is installed, your references will look like this:

Newtonsoft.Json package installed to the project
Figure 4: Newtonsoft.Json package installed to the project

Json.NET library is the Newtonsoft.Json library, so it gets added to the project under “From Packages” category. This has been added to the project, and you can then get to use it in your project. Before I do that, let me show you how to search for the project packages in NuGet.

Where on file system are they stored?

This is another interesting thing, in Mono environment, the project packages that are downloaded from the galleries, are stored in the same package directory. For more on package directories, please read my previous post: Guide for building C# apps on Ubuntu: Project files and output. In the directory, a new folder, “packages” would be created and then your newly referenced package will be stored there for later references.

folder in the project directory
Figure 5: “Packages” folder in the project directory.

You will find the code and other files all in this folder. This is done so that you can use the package later, the packages in NuGet are online and thus you cannot bear to pay the price of network latencies each time you want to build the project, downloading the assemblies is the easiest method.

Searching for a library

Search bar is also provided in NuGet management window that you can use to search for the libraries. The list shows only the popular ones, but if you want to search for third-party average project libraries that are not in the use of people more often you can use the search box to search for those libraries and window would bring up the matches that it finds on the gallery. You can then select from those results and get what you want to install in your application project. For example, to install LoginKeys package, you can write the keyword in the search box and NuGet package manager would bring the results, so that you can select from the search results.

LoginKeys package being shown in the search results
Figure 6:
LoginKeys package being shown in the search results

You can see that the GUI window has shown the results, and also, it has provided me with the details so that I can know that I am installing the correct version of library, from the author I trust (do I trust myself? Nah!), then I can follow the same steps and install this package to my project too.

Updating and restoring packages

In Mono, updating and restoring the NuGet packages is also very simple, and takes just a click! Yes, have a look below:

Options to update or restore the NuGet packages
Figure 7: Options to update or restore the NuGet packages.

These are the options provided, to update or restore the packages, just click on the option and Mono would do it for you! There are many other options also available along with these.

Referencing the package namespace

Once you have added the package to your project, you need to reference it in your source files too, although the assembly is available, but to be able to reference the functions in your source file, you also need to add the using statement in your project. We have the Newtonsoft.Json package, to be able to consume the library, we would need to reference it and then build the application.

Have a look at the following code:

Compiler error shown
Figure 8: Compiler error shown

The problem, you are right, is raised because we have not yet referenced the namespace to include the object JsonConvert in our project. So, we write the code like this:

  1. using System;  
  2. using Newtonsoft.Json; // Adding the reference  
  3. namespace NugetPackages  
  4. {  
  5.     class MainClass  
  6.     {  
  7.         public static void Main(string[] args)  
  8.         {  
  9.             /* 
  10.              * We do not need to create a new class, instead 
  11.              * we can create anonymous types.  
  12.              */  
  13.             object anon = new  
  14.             {  
  15.                 Name = "Afzaal Ahmad Zeeshan", Age = 20  
  16.             };  
  17.             // Now the function would serialize the anon object  
  18.             var serialized = JsonConvert.SerializeObject(anon);  
  19.             // We then print the JSON format on screen.  
  20.             Console.WriteLine(serialized);  
  21.         }  
  22.     }  
  23. }  
This code would compile and execute safely. The output of this code is like this (I think, you already know),

Result of the above code
Figure 9: Result of the above code

Pretty simple, I don’t need to explain it at all, because Newtonsoft.Json namespace is already very famous and widely used package.

Points of Interest

This is another post in the series of “Programming C# on Ubuntu” category of articles. In this post, you were taught to use NuGet packages in your Mono projects on Ubuntu. The post was intended for absolute noobs and beginners in Mono programming on Ubuntu, and hopefully, you have been taught to program applications on Ubuntu.

This was a short descriptive post for NuGet packages only. In later post,s you will be taught to develop libraries and re-use them in other projects. Stay tuned for the upcoming posts.

 

Up Next
    Ebook Download
    View all
    Learn
    View all