RWC: Linux Mono-culture

By

Leigh Dyer shows you how to free your ASP.NET applications from Microsoft's grasp with Mono

When Microsoft embarked upon its .NET strategy a few years ago, the company added the .NET name to just about every product in sight. Suddenly, .NET was a database, a server platform, and a web services infrastructure, to name just a few products. After much confusion, Microsoft removed the .NET name from its server products, leaving us with the core of the .NET initiative -- a software platform designed to make life easier for developers.

The .NET platform is in many ways similar to Java. It features its own programming language, called C#, which is compiled to a CPU-neutral bytecode format called IL. These bytecodes are executed inside a virtual machine, which translates them into native instructions using Just-In-Time (JIT) compilation techniques. The concept is quite similar to emulators like QEMU that use dynamic recompilation to improve performance, but because IL is specifically designed for virtual execution, the performance reaches close to native speeds. Along with the virtual machine comes an extensive set of class libraries to help developers do things like accessing databases or building GUIs.

The main difference between Java and .NET is that while Java is built largely around the Java language, .NET is designed to support many languages. Because each supported language is compiled down to the same bytecode format, classes and libraries written for .NET get instant cross-language compatibility, eliminating the need for language bindings.

As with Java, the use of a virtual bytecode format allows for easy portability of code, since the code can be run on any system that implements the .NET platform. For Microsoft, it's a step towards unifying development across various versions of Windows, from desktop PCs to mobile and embedded devices such as PDA, mobile phones and set-top boxes. However, with a compatible .NET implementation, applications could be made to run on any platform.

Linux and .NET
The Mono project brings the .NET platform to Linux and UNIX systems, with a fast JIT virtual machine and a portable re-implementation of Microsoft's class libraries. Some parts of the class library, such as the standard GUI classes, are missing because they're very Windows-centric and hence hard to reproduce in a portable way. However, most of the other important bits and pieces are fully functional, including support for ADO.NET database access and ASP.NET web services. This makes the combination of Mono and Apache a viable alternative for Windows and IIS for running ASP.NET services.

However, Mono's primary goal was not to bring Windows technologies like ASP.NET to Linux. Rather, Mono's developers have put much of their time and effort in to developing bindings for native Linux technologies, particularly the GNOME libraries. This means that while Mono may not be able to run Windows .NET GUI applications, its performance and ease of development make it a great platform for developing Linux-native GUI applications.

Installation
Installing Mono can be a trickier than installing a Java runtime since various parts of the platform are split into separate packages. Fortunately, it's not too painful as the packages are available for most distributions. Packages are available from the Mono website (www.mono-project.com) or can be downloaded via Ximian/Novell's Red Carpet utility. If, like me, you run Debian unstable, you'll find packages for everything as part of the distribution.

Running .NET applications from the command line is done using the mono command, in much the way that you'd use the python or perl commands to run scripts written in those languages. Other important commands are mcs, the Mono C# compiler, and mbas, the experimental Visual Basic.NET compiler, which you can use to compile source code in to .NET binaries.

Interestingly, Mono's compilers and other utilities are all .NET applications - only the core virtual machine is written in C. In the early days of Mono's development, while the runtime and class libraries were quite immature, work on the C# compiler and the class libraries was done on Windows using Microsoft's .NET implementation. Having .NET applications as core parts of the platform proved to be a great motivator though, with Mono quickly reaching the point where it could compile and run its compiler.

If command line based development doesn't take your fancy, check out MonoDevelop, a great IDE which you can grab from www.monodevelop.com. It provides easy source code editing with code completion, a project manager, documentation browser and integrated debugger.

There's a variety of other applications available for Mono as well. Muine (http://muine.gooeylinux.org/) is a simple music player with a nice, clean interface that has a very neat party trick - automatic download of album covers from Amazon. Other notable apps include Blam, an RSS feed reader (http://micke.hallendal.net/archives/000102.html), and a star chart viewer called SkyNET (http://sky-net.sf.net/).

After using Mono for a while, you'll probably get sick of running programs using the mono command. Fear not though, since there's a little-known Linux feature ready to rescue you: miscellaneous binary format support. This lets you specify applications to run different types of files, much like file associations in Nautilus or Konqueror. Try these commands:

modprobe binfmt_misc
mount -t binfmt_misc none /proc/sys/fs/binfmt_misc
echo ':CLR:M::MZ::/usr/bin/mono:' > /proc/sys/fs/binfmt_misc/register

If everything goes well, running Mono EXE files should work transparently. Put these commands into an appropriate startup script and you should be set. If you're running Debian though, you shouldn't have to bother - the Mono packages are built to set up miscellaneous binary support automatically.

ASP.NET and Linux

One of Mono's most compelling features is its ability to run ASP.NET applications, giving Windows web application developers a bit more choice when deploying their work. Mono's ASP.NET implementation is built into its class libraries, making it easy to embed a web server in any .NET application, though it also comes with a reference web server called XSP.
Getting XSP running is simple. Put some files in a directory, change into that directory, and run xsp.exe using the mono command (or directly, if you've set up miscellaneous binary support). Your files should now be available from a web browser by connecting to http://localhost:8080/.
Basic ASP.NET is quite similar to PHP, JSP, and everything else that ends in SP (except, perhaps, the Gameboy Advance SP). An ASP.NET page is just like a plain HTML page, but with code sections contained in <% and %>tags. As a very simple test, put the following into a text file called test.aspx:

<%@ Page Language="C#" %>
<%
Response.Write("

Hello from Mono!

");
%>

It's a trivial example, but if it wor

This feature appeared in the October, 2004 issue of PC & Tech Authority Magazine
Tags:

Most Read Articles

How to cast from Windows 10 to a smart TV with 5KPlayer

How to cast from Windows 10 to a smart TV with 5KPlayer

How to: Fix a stuck Windows 10 update

How to: Fix a stuck Windows 10 update

Review: Alienware AW3418DW 34-inch curved gaming monitor

Review: Alienware AW3418DW 34-inch curved gaming monitor

Is a Nintendo Switch Virtual Console coming as Nintendo bans retro ROM resources?

Is a Nintendo Switch Virtual Console coming as Nintendo bans retro ROM resources?

Would you like to receive

Our Newsletter?

Most popular tech stories