Get Started with Mono

Tweet

Here’s a disclaimer: I avoid Linux and am no Linux expert by any means. I shudder at the thought of black screens with a flashing cursor. I find myself moving my mouse around trying to find an icon to click or a menu to select.

It’s from such a perspective that this article will demonstrate how anyone (even I!) can get Mono up and running on a fresh, clean Linux box. I’ll walk through my experience of installing the package, and discuss all the components needed to run Mono.

What Is Mono?

Mono is an open source project used to code much of the .NET framework in cross-platform executable code. Sponsored by Novell, Mono gives developers the chance to develop ASP.NET and Windows Forms applications not only for Windows, but now, principally, for Linux.

While they’re not yet complete, applications written for the standard Microsoft .NET framework can now be ported and run on Linux through Mono. However, holes within the framework implementation on Mono mean you’ll still need to check that your application doesn’t use any services that are not yet implemented.

What’s Usable in Mono?

Currently, Mono is in development, but the project has reached some significant milestones that make it suitable and stable enough for deployment today. The C# compiler is the only fully featured compiler for Mono. Yes, that’s right: VB.NET and J# developers will need to switch to C# to fully use Mono. Watch the progress of the VB.NET compiler here.

The compiler itself is written in C#. You can download all 1.7 million lines of C# code and compile this yourself, or, as we’ll see shortly, you can use one of the many distributions to ease the installation process.

ASP.NET is fully-featured and supported within Mono. In fact, it’s the great strength of Mono at present. You can build and deploy both Web forms and Web services using either the built-in Web server that ships with Mono (XSP) or through an Apache modification, Mod Mono. For those who are uncomfortable using Windows and IIS to host applications, Mono provides a viable alternative.

Windows Forms is currently under development, but functionality is progressing. Though complex WinForm applications, such as Web Matrix, are currently unavailable, there are alternatives to WinForms in Mono that build GUI applications. Gtk# and #WT are wrappers to the popular GUI tools on Linux. WinForms itself is being built as a library extension to Wine, the Win32 implementation on Linux. The project’s progress is documented here.

ADO.NET and the System.Data classes are also fairly mature; however, they aren’t at production level at the time of writing. This is one of the largest projects within Mono, and at present you can connect to a wide variety of databases. Native support is provided for many of the databases usually associated with Linux, such as PostgreSQL.

Mono has successfully been used in commercial and heavy-use applications. Novell used the tool to build two of its products, iFolder and ZenWorks. Also, SourceGear has used Web services deployed in Mono within its Vault application.

Getting Started: Download Mono

Mono is available in many packages. You can download the latest source code build and a daily snapshot source code build, through CVS, an RPM package, or a Red Carpet install for those with Xiamian Desktop. By far the easiest to use is the Red Carpet system, which, while similar to RPM, offers good versioning control, allowing you to upgrade Mono on your machine very easily.

The Mono download page details the various packages and how they can be downloaded, as well as specific packages for specific varieties of Linux.

http://www.mono-project.com/downloads/

I downloaded the Mono Runtime tarball and used the following command to unpack the distribution:

tar -zxvf mono-1.0.tar.gz

Once the tarball was extracted, I could start the installation process using:

./configure
make
make install

It was at this point I realised I needed to upgrade pkg-config on the system as the installation spat out some errors. I found the RPM distribution for this, and installed it using the following command:

rpm -Uvh pkgconfig-0.15.0-4mdk.i586.rpm

The make process now worked without a hitch.

Hello World in C# Running on Linux

It’s always a good thing to use the cliched "Hello World" example to test an installation!

Open your favourite Linux text editor (I used vi) and enter the following simple C# application code:

public class HelloWorld  
{
 static void Main()  
 {
   System.Console.WriteLine("Hello World!");
 }
}

Save this file as HelloWorld.cs and compile the class with the Mono C# compiler:

mcs HelloWorld.cs

In the directory to which you saved HelloWorld.cs, you should now see a HelloWorld.exe file. This is standard MSIL code that can be executed on any computer on which the .NET framework is installed, including Windows.

There are two ways to run Mono applications. One method is to use "mint," which is an interpreter for the MSIL bytecode. Mint does not compile and run your applications into native machine code, hence it is not a JIT runtime. "Mono" however is a JIT runtime, which compiles bytecode when first requested into machine code native for the platform and processor for which it was designed.

This means that, for performance, the Mono application is much faster than mint, though it’s not as portable as it is tied to the particular operating system. Mint, on the other hand, is far more portable and, as it’s written in ANSI C, may be used on a multitude of deployment platforms.

To run our Hello World application, we can use the following command, which invokes Mono:

mono hello.exe

We use the following command to invoke mint:

mint hello.exe
Dishing Up ASP.NET

Mono comes with its own Web server, ready to dish out your ASP.NET applications. Mono. XSP is very easy to use and makes for a simple ASP.NET Web server that is almost akin to the Cassini server that ships with Web Matrix on Windows. For more dominance, you can download a module called mod_mono that allows Apache to support ASP.NET. In this article, however, we’ll examine the formation of a simple Web application and host it using XSP.

I must admit that I cheated when I created the code for the Web application: I used Visual Studio to create the Web application and its associated files. Then, I copied the code over to the Linux box that was ready to host.

For our example, we’ll create a Web page with a simple button and a label. When the button is clicked, the label will show that the user clicked the button.

<%@ Page Language="C#" %>  
<script runat="server">  

void Button1_Click(object sender, EventArgs e)  
{  
  titleTag.InnerText = "You clicked the button!";  
}  

void Page_Load(object sender, EventArgs e)  
{  
  if (!IsPostBack)  
    titleTag.InnerText = "Page has loaded";  
}  

</script>  
<html>  
<head>  
<title runat="server" id="titleTag"/>  
</head>  
<body>  
<form runat="server">  
  <asp:Button id="Button1" OnClick="Button1_Click" runat="server" Text="Button"/>  
</form>  
</body>  
</html>

You can save this file to a directory that will act as your root for the Web application.

If you are using codebehinds for your application, you will also need to compile the source files using the Mono compiler, in order to obtain the compiled assembly for the application. Just as with ASP.NET on Windows, you’ll need to drop this into a /bin directory on the root.

XSP, by default, listens on the 8080 port so as to not interfere with Apache; however, you can set up the server, via the command line, to listen at a different port. You can also specify the root directory of the application you wish to host:

xsp.exe  [--root rootdir]  
               [--port N]

With the server running, you can access your page through any standard Web browser. And, there we have it: ASP.NET served over Linux!

Third Party Tools

There are numerous tools available to aid your developments in Mono so that, unlike me, you don’t need to resort back to Visual Studio.

  • MonoDevelop:
    With GUI features still lacking in Mono, MonoDevelop is really pushing the current implementations to show what can be achieved in Mono. Resembling #develop for Windows, MonoDevelop supports syntax highlighting and the compilation of projects from an easy-to-use interface. However, this tool is still at an early stage of development and presently lacks the features needed to make it a truly useful instrument.
  • Eclipse:
    Billed as "a kind of universal tool platform — an open extensible IDE for anything and nothing in particular," Eclipse is a great solution for developing Mono applications. By downloading and installing the Improve C# plugin for Eclipse you can have a fully-featured and free Java based IDE for your Mono developments.
  • MonoDoc:
    This is a browser for the Mono documentation. It isn’t installed by default through the standard Mono packages, but it is a life saver for those needing to check whether certain APIs and parts of the framework are available in Mono.
Summary

From my study of Mono, I’ve come to understand that this is a very important project for .NET. The release of .NET from the confines of Microsoft operating systems will allow it to expand within communities that are normally off-limits to Microsoft technologies.

Mono can only grow stronger, and perhaps in the near future, .NET developers will be able to develop for Linux as easy as we develop for Windows today.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments