Building Cross-Platform .NET APP Using Mono

What is Mono?

After release of the .NET Framework people started discussion of its non-Microsoft Platform portability. In other words, can a Microsoft .NET Framework based application run on non-Microsoft operating systems, such as UNIX, Mac and various Linux distributions? To help developer's port .NET Code to non-Microsoft platforms the "Mono" project is the solution. Mono is an open-source implementation of Microsofts .NET Framework that allows developers to execute .NET centric applications on other platforms such Mac and Linux. Using such open-source implementations of .NET you can create, compile and execute .NET assemblies on operating systems other than the Microsoft platform.
 
At present, Mono is compatible with .NET 4.0 and C# 4.0. In addition, Mono provides an open-source distribution of the Silverlight API named Moonlight. The browser that runs under Linux can host and run Silverlight web applications. Mono features sets support numerous programming language's compilers as in the following;

Compilers Supported Languages
Vbnc Visual Basic.NET 
mcs,gmcs C#
Ilasm Mono CIL

Mono Prerequisites

  • Mono 2.10.x GTK for .NET (mono)
  • Development IDE (Xamarin Studio or MonoDevelop)
  • Mono tools
  • VMware 
  • One of the supported OS (OpenSUSE, Linux, Fedora ,Mac, Backtrack) running on VMware

Mono Development Environment Configuration

Go to the Mono Download website and go the download tab. Here, you can download various types of installers for your choice of platforms such as Mac, Linux and Windows.  On the website select Windows and then choose “Mono for Windows ,Gtk#” as shown in the image below.

download Mono

Once you finish with the setup program, by default Mono is installed under Program Files\Mono-version. You run the vast variety of Mono development tools from the command line. To test whether the Mono is installed properly or not, just run the following command in the in-built command prompt of Mono as:
 
command prompt of Mono

To have a Visual Studio like IDE feel you can install the Xamarin Studio IDE that allows you to develop a variety of solutions such as ASP.NET, console based and Android apps by choosing the C# or VB.NET programming languages.
 
choosing Csharp

Mono Tools

Mono provides various development tools for the use of .NET Developers that are functionally equivalent to .NET SDK tools.
 
Mono Utility Description
Gacutil It interacts with the global assembly cache.
Ilasm The CIL disammembler.
Al Build multifile assembly.
Wsdl It generates client-side proxy code for web services.
Disco It discovers the URL of web srvices.
Monop It displays the definition of a specified type
Glade It is used to build GTK# graphical applications.
SQL It allows you to interact with a relational database using ADO.NET providers.

Let's Build an application under Mono
 
To develop your first Mono Framework based console application with Xamarin Studio, follow these steps.

  1. Open Xamarin studio from Start Menu.
  2. Go to File -> New -> Solution. Here choose the Console Application in C# language. Select the solution Directory and finally create a project named "TestConsoleApp".

    create a project

  3. Then, you will find the following working area with entire solution directories as:

    solution directories

  4. You can code just like you code in .NET.  

    1. using System;  
    2.    
    3. namespace TestConsoleApp  
    4. {  
    5.     class MainClass  
    6.     {  
    7.         public static void Main(string[] args)  
    8.         {  
    9.             Console.WriteLine("Welcome! First Program");  
    10.             Console.ReadKey();  
    11.         }  
    12.     }  
    13. }  
  5. Finally build the project using F8 and run it with F5 to see the output.
  6. If the solution is compiled successfully, a TestConsoleApp.exe file is created.

    file created

  7. Now,  you can invoke the TestConsoleApp.exe into the Mono runtime engine by specifying the name of The executable as an argument to Mono as:
     
    Mono TestConsoleApp.exe
     
  8. You can also view the assembly manifest using monodis.exe as in the following:

    assembly manifest

Cross-Platform Nature Testing under Linux Environment

Now let's configure the mono framework over an open-source Linux distribution operating system backtrack to execute the .NET application. So, first download the Mono binaries using the wget utility and execute these series of commands to configure it properly as:

  1. mkdir   MonoTest
  2. cd  MonoTest
  3. wget --no-check-certificate https://raw.github.com/nathanb/iws- snippets/master/mono-install-scripts/ubuntu/install_mono-3.0.sh
  4. chmod 755 install_mono-3.0.sh
  5. ./install_mono-3.0.sh

Or we can install Mono runtime using another method. Just open the Backtrack terminal and execute these commands as follows:

Sudo apt-get install mono-runtime
Sudo apt-get install mono-mcs
Sudo apt-get install mono-gmcs

Note: Please ensure that you are connected to an internet connection during execution of the commands mentioned above. Be mindful that while installing, it might prompt you to install an extra dependent package.

package

Now export the previously created .NET assembly TestConsoleApp.exe to backtrack via FTP, Telnet or other ways.  Now let's execute our Console Application that we created previously.
 
assembly

Let's Do a Windows Forms application with Mono

Let's create a standard Windows Forms application that will have some UI Controls and code to execute the logic. I will be placing two text boxes and one button control. This purpose will be to validate the correct user name and password against pre-defined values in the C# code. 

  1. using System;  
  2. using System.Drawing;  
  3. using System.Text;  
  4. using System.Windows.Forms;  
  5.    
  6. namespace TestWindowsWithMonoSoftware  
  7. {  
  8.     public partial class Login : Form  
  9.     {  
  10.         public Login()  
  11.         {  
  12.             InitializeComponent();  
  13.         }  
  14.    
  15.         private bool UserAuth(string usr, string pwd)  
  16.         {  
  17.             string USR = "MyPassionFor.NET";  
  18.             string PWD = "1234";  
  19.             bool status = false;  
  20.    
  21.             if (usr == USR && pwd == PWD)  
  22.             {  
  23.                 status = true;  
  24.             }  
  25.    
  26.             return status;  
  27.         }  
  28.    
  29.         private void btnLog_Click(object sender, EventArgs e)  
  30.         {  
  31.             if (UserAuth(txtUser.Text, Password.Text))  
  32.             {  
  33.                 label3.Text = "Login Successful";  
  34.             }  
  35.             else  
  36.             {  
  37.                 label3.Text = "Login Failed";  
  38.             }  
  39.         }  
  40.     }  
  41. }   

It is now time to compile this Windows Forms based project under Mono. First open a Mono command prompt and compile it using the gmcs utility as follows:

gmcs utility

Finally, we need to transfer this file champu.exe to the Linux operating system using any conduit such as a FTP server or Samba server. Once we transfer this executable file to a Linux distro, open-up the terminal under Linux and execute this command as follows;
 
transfering file

As you can see we have successfully executed a C# created .exe package under a Linux platform using Mono.

Next Recommended Readings