Click here to Skip to main content
15,893,337 members
Articles / Programming Languages / C#

Guide For Building C# Apps on Ubuntu: NuGet Packages

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
21 Jan 2016CPOL7 min read 13.3K   3   1
In this post version, I have talked about NuGet packages and their management in Mono environment.

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 towards 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 would 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

So that you can have a good understanding of NuGet packages, package management and how to use them in your projects, 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

Configuring the IDE

The 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.

Screenshot (5864)

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”.

Screenshot (5865)

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

This would allow you to alter the sources and other settings, you can also change the names for the galleries. However, that is pretty much 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 listview itself. It is very much simple to install the package.

I have demonstrated the procedure in the following image:

Screenshot (5867)

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 would 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 would download and install the packages, in a while. Once that is installed, your references would look like this:

Screenshot (5868)

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.

Screenshot (5873)

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.

Screenshot (5869)

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:

Screenshot (5874)

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 are having 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:

Screenshot (5871)

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:

C#
using System;
using Newtonsoft.Json; // Adding the reference

namespace NugetPackages
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            /*
             * We do not need to create a new class, instead
             * we can create anonymous types. 
             */
            object anon = new { Name = "Afzaal Ahmad Zeeshan", Age = 20 };

            // Now the function would serialize the anon object
            var serialized = JsonConvert.SerializeObject (anon);

            // We then print the JSON format on screen.
            Console.WriteLine (serialized);
        }
    }
}

This code would compile and execute safely. The output of this code is like this (I think, you already know).

Screenshot (5872)

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 somehow a short descriptive post for NuGet packages only. In a later post, you will be taught to develop libraries and re-use them in other projects. Stay tuned for the upcoming post. :-)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan
Afzaal Ahmad Zeeshan is a computer programmer from Rabwah, Pakistan, currently living in The Netherlands, likes .NET Core and Node.js for regular everyday development. Afzaal Ahmad works at Adyen as a Developer Advocate.

He is an expert with Cloud, Mobile, and API development. Afzaal has experience with the Azure platform and likes to build cross-platform libraries/software with .NET Core. Afzaal is an Alibaba Cloud MVP, twice he has been awarded Microsoft MVP status for his community leadership in software development, four times CodeProject MVP status for technical writing and mentoring, and 4 times C# Corner MVP status in the same field.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Santhakumar M24-Jan-16 6:19
professionalSanthakumar M24-Jan-16 6:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.