mod_mono

mod_mono is an Apache 2.0/2.2/2.4.3 module that provides ASP.NET support for the web’s favorite server, Apache.

The module passes off requests for ASP.NET pages to an external program, mod-mono-server, which actually handles the requests. The communication between the Apache module and mod-mono-server is established using a Unix socket or a TCP socket.

The most simple scenario uses Apache as the HTTP server front end which passes the requests to mod-mono-server to handle, the following diagram illustrates how this setup works:

Modmono-basic-setup.PNG

Mod_mono is an Apache module that is hosted inside Apache. Depending on your configuration the Apache box could be one or a dozen of separate processes, all of these process will send their ASP.NET requests to the mod-mono-server process. The mod-mono-server process in turn can host multiple independent applications. It does this by using Application Domains to isolate the applications from each other, while using a single Mono virtual machine.

Although AppDomains provide an adequate level of isolation, mod_mono can also be configured to route different urls to different mod-mono-server processes. You can use this to:

  • As an ISP, isolate different customers to different processes
  • Allow experimental code to run in one server, independent of production code.
  • Allow the kernel to enforce different isolation rules for different processes (for example with AppArmor or SELinux)
  • Setup different CPU, Disk and memory quotas to different processes

Modmono-multiple-servers.PNG

For example, in the setup above, the various Apache workers will route requests to /forums and /support to a mod-mono-server that is used by the “community” user on the server. This mod-mono-server will still use two separate AppDomains to isolate the forums software from the support software.

The /blog url will be sent to the mod-mono-server that is ran by the marketing group, the /api url will be sent to the mod-mono-server for user engineering while the application in /testing will be handled by an experimental version of Mono.

Requirements

You will need apache , the web server, installed.

From Downloads you will need mono, xsp and mod_mono.

Distribution-Specific Documentation

If you are using one of these linux distributions, you should look at the corresponding documentation before reading the rest of this page, as some things are different on every distro. It is also always recommended to use your distribution’s official packages when available, rather than compiling from source.

Easy Configuration of Mod_Mono

When you installed XSP, a bunch of sample ASP.NET pages and web services were installed too. If the prefix used to configure XSP was /usr, the sample files are located in /usr/lib/xsp/test.

If your needs are not very complicated, all you need is to use AutoHosting, this basically means that you load the mod_mono.conf file, like this in your Apache configuration file:

Include /etc/apache2/mod_mono.conf

And applications will start to be served. To try it out, copy the /usr/lib/xsp/test directory to your Apache home (in openSUSE this is /srv/www/hdtocs).

It is recommended that you create a directory per application that you want served. This will allow you to xcopy deploy your applications from Windows to Linux if you want to.

More on automatic configuration of mod_mono applications is in AutoHosting.

mod_mono.conf loads the mod_mono module, associates ASP.NET file extensions with the ASP.NET MIME type and adds index.aspx, Default.aspx, and default.aspx as automatic directory index pages (with the DirectoryIndex directive). If you don’t include mod_mono.conf in your main Apache configuration, you will at least need to have the mod_mono.so module loaded with:

LoadModule mono_module /usr/lib/httpd/modules/mod_mono.so

For more detailed configuration and manual tuning keep reading.

mod_mono will automatically launch mod-mono-server and start the web application on the first request for a page handled by mod_mono. In the early days of mod_mono, you had to start mod-mono-server by yourself, ensuring that it had all the parameters needed to understand the requests forwarded by the module. This is still an option for those who want mod-mono-server to have a separate life cycle from apache, but you will probably prefer to use mod_mono built-in ability to start and stop mod-mono-server for you.

Apache performance tweaks

You might want to consider modifying the apache configuration so that mod_mono performs better.

Keep alive

The HTTP/1.1 protocol defines a theoretically performance-improving mechanism – keep alive. What it means is that the web server can keep the connection open for a while for the client to request several resources over the same connection. In reality, however, very often that feature becomes a performance killer. The reason is that each keep alive session requires the server to keep the process (or thread) busy in order for the timeout to happen or for the client to close the connection. Apache contains configuration directives to set the number of maximum clients serviced, the number of threads/processes (servers) to spawn etc (for more information see http://httpd.apache.org/docs/2.2/). Each time a keep alive session is open and the client does not use it, Apache will still keep the server process/thread busy and thus it won’t be able to accept another connection on that server – which will limit the throughput. There are two solutions to this problem. The better one, in my opinion, is to turn keep alive off completely by putting this directive somewhere in your Apache config:

KeepAlive Off

If, however, you want to use keep alive, you might try decreasing the session timeout to a small value (for instance 2 seconds) by putting this directive in the Apache configuration:

KeepAliveTimeout 2

MPM worker

Apache 2.x comes with several processing modules (that is – servers, so-called MPMs for Multi Processing Module) implementing different server models. The one installed by default by most distributions is the prefork MPM which implements the traditional, one OS process per server, model and is the same what in the earlier Apache versions. Another module usually available with your distribution is the worker MPM. That module implements a mixed process/thread model which spawns several processes as well, but each of them can create a configurable number of service threads. It is advisable to use the worker MPM with mod_mono. Unfortunately, PHP seems to not necessarily work with the worker MPM, so you may not be able to go this route.

Configuring Mod_Mono

When AutoHosting does not fit your needs, you will need to include several mod_mono Apache directives in your main Apache configuration file (often /etc/httpd/conf/httpd.conf, or the like in /etc/apache2) to get the site running.

Mod_Mono Configuration Tool

The Apache mod_mono configuration tool can generate a configuration for name-based Virtual Hosts (i.e., how this site is configured to traffic to mono-project.com, http://www.mono-project.com, etc), and configurations for ASP.NET Applications (what IIS traditionally referred to as a Virtual Directory), such as the mod_mono configuration application served at http://go-mono.com/config-mod-mono/

In the simplest case, you shouldn’t have to supply the tool with anything more than a server or application name; the tool will suggest a path where you can deploy your application. With the intention of making developing and porting applications as painless as possible, the default configuration will set mod_mono to run with both mono debugging and platform abstraction enabled. These may not be the best options for production web sites, so consider disabling those features if/when you no longer need them.

Once you’ve completed the form, the tool will generate a configuration you can save to disk (/etc/apache2/conf.d/ on SUSE/openSUSE). To begin serving your newly configured application, simply restart apache:

        sudo /sbin/service apache2 restart

Manual Mod_Mono Configuration

The following assumes you have included mod_mono.conf in your main configuration file as described above. Further, it is important (as of Mono 1.2.5) to place the remaining mod_mono directives after the User and Group directives. They can just go at the end, or inside VirtualHost sections.

A basic setup is as follows (with line numbers added for convenience):

1   MonoAutoApplication disabled
2   AddHandler mono .aspx .ascx .asax .ashx .config .cs .asmx .axd
3   MonoApplications "/:/home/username/www"

The first line disables the AutoHosting feature. (If inside a VirtualHost section, it disables it just for that virtual host).

The second line instructs Apache that processing of files with .aspx, etc. extensions should be delegated to mod_mono (rather than Apache processing them itself as plain-text files).

The third line instructs mod_mono that an ASP.NET application exists at the root directory of the web site (i.e. at http://www.example.com/), and that this virtual path corresponds to the physical path on disk of /home/username/www. Normally, the physical path will match whatever Apache would map the given virtual path to. So if the virtual path is /, as in this example, the physical path matches what is in the DocumentRoot directive for Apache. This is important because in that virtual path, Apache itself will continue to serve images, static HTML, and other files, based on the physical path it knows in DocumentRoot, while mod_mono will handle files with .aspx, etc. extensions (or whatever was specified in AddHandler) based on the physical path provided in the MonoApplications directive.

Here is another configuration that sets up the ASP.NET test suite that comes with mod_mono.

Let’s say you want those file to be available under the virtual path /test. Edit your httpd.conf file (hint: /etc/httpd, /etc/apache2) and append the following lines (remove the numbers ;-):

1   Alias /test "/usr/share/doc/xsp/test"
2   MonoApplications "/test:/usr/share/doc/xsp/test"
3   <Location /test>
4       SetHandler mono
5   </Location>

Unlike the first example, this example assumes that the virtual path “/test” does not already correspond to the physical path /usr/share/doc/xsp/test. The Alias directive is a standard Apache directive to map a virtual path to a physical path. The second line creates an ASP.NET application in something other than the root virtual path. Lines 3-5 instruct Apache that absolutely all files in the /test virtual path are to be handled by mod_mono. (mod_mono can handle non-ASP.NET pages as well. It will just send other files to the client directly without special processing.)

Now start/restart Apache and browse http://hostname/test/index.aspx (where hostname is the name of the server, or 127.0.0.1 if you’re running Apache locally). If you cannot see the test page, go to the troubleshooting section. Otherwise, welcome to ASP.NET!

Okay, it worked. But, what happened? When you started apache, mod_mono launched mod-mono-server. Later, when you requested any page under /test, mod_mono connected to mod-mono-server, forwarded the request data and retrieved the response that is sent to your browser. Finally, if you stop apache, mod_mono will tell mod-mono-server to die.

It is possible to put these directives inside a VirtualHost section as well.

If your site uses .NET 2.0 classes, you will need to instruct mod_mono to spawn the .NET 2.0 version of mod-mono-server, instead of the default .NET 1.1 version. To do that, use the following, changing the path to mod-mono-server2 as needed:

MonoServerPath /usr/bin/mod-mono-server2

If you use AddHandler you might want to let mod-mono-server know about your DirectoryIndex directive by doing something like this in your web.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="MonoServerDefaultIndexFiles" value="mywierdindexfile.html,index.aspx" />
</appSettings>
</configuration>

More on Applications

How do you go about making mod_mono handle several applications? Let’s try the easiest option. You will need this in your httpd.conf:

1   Alias /test "/usr/share/doc/xsp/test"
2   Alias /personal "/home/user/mypages"
3   AddMonoApplications default "/test:/usr/share/doc/xsp/test,/personal:/home/user/mypages"
4   <Location /test>
5       SetHandler mono
6   </Location>
7   <Location /personal>
8       SetHandler mono
9  </Location>

The significant change is in line 4:

AddMonoApplications "/test:/usr/share/doc/xsp/test,/personal:/home/user/mypages"

    ...is equivalent to...

AddMonoApplications "/test:/usr/share/doc/xsp/test"
AddMonoApplications "/personal:/home/user/mypages"

This way you can keep all the configuration related to an application in a separate file (Alias, AddMonoApplications, Location,…).

If you serve mod_mono applications in multiple virtual hosts, you can use this syntax:

AddMonoApplications "www.example.com:/:/home/exampledotcom/www"
AddMonoApplications "www.sample.com:/:/home/sampledotcom/www"

The above example instructs mod-mono-server to create two applications, one mapping http://www.example.com/ to /home/exampledotcom/www and the other mapping http://www.sample.com/ to /home/sampledotcom/www.

Multiple Applications, Multiple mod-mono-servers

You might want to run different applications in different instances of mod-mono-server. There are a number of reasons for doing this:

  • If you want to run a production and testing environments.
  • You need completely separate Mono instances running.
  • If you don’t want Session or Application level objects to be shared among applications or that you want to set certain memory or CPU usage restrictions for an application.

Let’s see the configuration needed to achieve this separation for the two applications in the previous example. (Alias directives are omitted for brevity, and this example assumes the AddHandler directive has been used to associate ASP.NET file extensions with mod_mono.)

1   MonoApplications testing "/test:/usr/share/doc/xsp/test"
2   <Location /test>
3      MonoSetServerAlias testing
4   </Location>
5
6   MonoApplications personal "/personal:/home/user/mypages"
7   <Location /personal>
8      MonoSetServerAlias personal
9   </Location>

When (Add)MonoApplications is given two arguments, the first argument is understood as a name or alias for a particular instance of the mod-mono-server backend. With this configuration mod_mono will start two instances of mod-mono-server whose aliases are ‘testing’ and ‘personal’. The ‘testing’ instance will handle /test and the ‘personal’ instance will handle /personal.

MonoSetServerAlias tells mod_mono which instance of mod-mono-server will be used to process the requests for this <Location>. It can be used with apache <Directory> too.

The alias used when no alias is provided, as in the earlier examples, is “default”. All of the mod_mono directives below that show an alias as the first argument also can be specified by leaving the alias out, in which case the “default” alias is used.

Control panel

mod_mono provides a simple web-based control panel for restarting the mod-mono-server, which is useful when assemblies need to be reloaded from disk after they have been changed. To activate the control panel, place the following in your httpd.conf:

<Location /mono>
  SetHandler mono-ctrl
  Order deny,allow
  Deny from all
  Allow from 127.0.0.1
</Location>

The Order/Deny/Allow access controls above restrict access to the control panel to the computer with IP address 127.0.0.1. Replace this (or add more Allow lines) with the IP address of your own computer so that you can access the control panel. Note that anyone on the machine 127.0.0.1 will have the ability to affect any configured mod_mono applications. (These directives placed in a VirtualHost section allow access to only mod_mono applications configured within that virtual host.)

The control panel is then accessible at http://yourdomain.com/mono. It allows you to:

  • Restart mod-mono-server backends for all or individual applications.
  • Stop or resume handling pages for all or individual applications.
  • See how many concurrent requests are currently being processed.
  • See how many requests are waiting to be processed, according to the MonoMaxActiveRequests directive (explained below).
  • See how many requests have been served since the last restart if the MonoAutoRestartMode Requests directive is used (explained below).
  • See how much time has elapsed since the last restart if the MonoAutoRestartMode Time directive is used (explained below).

Advanced options

Automatic restart of the mod-mono-server backend

mod_mono can automatically restart the Mono (mod-mono-server) backend that is handling requests after a certain amount of time. This is useful if you find that the mono process is growing indefinitely over time, or if you just need to make sure you clean house every so often.

There are two automatic restart methods: one based on time, and one based on the number of requests served. You can active them as follows:

# Auto-restart after three hours.
1 MonoAutoRestartMode Time
2 MonoAutoRestartTime 00:03

The time format above is DD[:HH[:MM[:SS]]].

# Auto-restart after 10,000 requests served.
1 MonoAutoRestartMode Requests
2 MonoAutoRestartRequests 10000

As with most other mod_mono directives, the first parameter to a directive can be the name or alias of a mod-mono-server. This is always optional and is omitted in the examples above.

Limiting the number of concurrent requests

The number of concurrent requests that can be processed by the mod-mono-server backend is limited by the size of the ThreadPool, and you could experience deadlocks when too many requests are going at once. As a result of the deadlocks, Apache child process instances that are processing requests get backed up until no more incoming HTTP connections can be made (even for any virtual host).

mod_mono will limit the number of concurrent requests that are passed off to mod-mono-server, and when the limit is reached, incoming requests wait for a certain amount of time until more requests can be passed off to the backend. The default limit of concurrent requests is 20, and the default limit of requests waiting to be passed off to the backend is 20. This should be just below the amount mod-mono-server can process without reaching the ThreadPool limit on a single processor machine.

To adjust the limits, use these directives:

1 MonoMaxActiveRequests 20
1 MonoMaxWaitingRequests 20

A value of zero disables the limits.

To adjust the number of threads in the threadpool, you have two choices: this can be either configured as part of your ASP.NET application configuration or it can be made global to Mono. you can do this by using the MONO_THREADS_PER_CPU environment variable, the default being 50 (25 on windows):

export MONO_THREADS_PER_CPU=2000

If you are using Mono from Apache to run ASP.NET, you can use the MonoSetEnv configuration option in Apache:

MonoSetEnv MONO_THREADS_PER_CPU=2000

For ASP.NET applications it’s also a good idea to tweak the default values found in machine.config, inside <system.web> section:

    <httpRuntime executionTimeout="90"
           maxRequestLength="4096"
           useFullyQualifiedRedirectUrl="false"
               minFreeThreads="8"
           minLocalRequestFreeThreads="4"
           appRequestQueueLimit="100" />

Setting hard memory and time limits

Here’s an example on how to set memory and CPU limits for a given server:

1   LoadModule mono_module modules/mod_mono.so
2   Alias /jeanette "/home/jeanette/web"
3   AddMonoApplications jeanette "/jeanette:/home/jeanette/web"
4   MonoMaxMemory jeanette 200000000
5   MonoMaxCPUTime jeanette 3600
6   <Location /jeanette>
7       MonoSetServerAlias jeanette
8       SetHandler mono
9   </Location>

Lines 4 and 5 set the maximum memory to be used (bytes) and the maximum CPU time consumed (seconds) by the ‘jeanette’ mod-mono-server instance. After reaching the limit, the OS will kill mod-mono-server. A new instance should start automatically on the next request. (But, JT notes that these directives don’t work at all for him.)

Unix and TCP sockets

We said that mod_mono and mod-mono-server can use a unix or a TCP socket to sent data back and forth. Use of unix sockets is the default, but you can use a TCP socket in case you have several computers running apache and one single machine providing mod-mono-server services.

The only parameter that you can control when using a unix socket is the file name. The directive is MonoUnixSocket:

    LoadModule mono_module modules/mod_mono.so

    Alias /julia "/home/julia/web"
    AddMonoApplications default "/julia:/home/julia/web"
    # When no MonoUnixSocket is provided, the default
    # is /tmp/mod_mono_server[_alias]
    # In this case, for the 'default' alias: /tmp/mod_mono_server
    <Location /julia>
        SetHandler mono
    </Location>

    Alias /jennie "/home/jennie/web"
    AddMonoApplications jennie "/jennie:/home/jennie/web"
    # In this case, alias 'jennie': /tmp/mod_mono_server_jennie
    <Location /jennie>
        MonoSetServerAlias jennie
        SetHandler mono
    </Location>

    Alias /juno "/home/juno/web"
    AddMonoApplications juno "/juno:/home/juno/web"
    # Uses a file under juno's home directory
-   MonoUnixSocket juno /home/juno/tmp/juno_server
    <Location /juno>
        MonoSetServerAlias juno
        SetHandler mono
    </Location>

You can set the file name to whatever you want as long as the user running apache has the necessary permissions to create and remove that file.

In order to run an instance of mod-mono-server that listens on a TCP socket, there’s a mandatory MonoListenPortdirective and an optional MonoListenAddress. See them in action:

    LoadModule mono_module modules/mod_mono.so

    Alias /jazmin "/home/jazmin/web"
    AddMonoApplications jazmin "/jazmin:/home/jazmin/web"
    # 'jazmin' mod-mono-server will be listening on
    # port 9000, address 127.0.0.1
-   MonoListenPort jazmin 9000
    <Location /jazmin>
        MonoSetServerAlias jazmin
        SetHandler mono
    </Location>

    Alias /joan "/home/joan/web"
    AddMonoApplications joan "/joan:/home/joan/web"
    # 'joan' mod-mono-server will be listening on
    # port 7000, any address (0.0.0.0)
-   MonoListenPort joan 7000
-   MonoListenAddress joan 0.0.0.0
    <Location /joan>
        MonoSetServerAlias joan
        SetHandler mono
    </Location>

MonoUnixSocket and MonoListenPort are mutually exclusive. Don’t use both.

Paths

In case it is needed, you can provide alternative locations for mod-mono-server. Other directories containing assemblies that mono could not find in the default search paths can also be specified. Mono needs a writable directory used by the windows I/O emulation layer that is usually in the user’s home .wapi directory ($HOME/.wapi). In mod_mono, the directory where .wapi is created is set to /tmp, but you can change that too.

    LoadModule mono_module modules/mod_mono.so

    Alias /jane "/home/jane/web"
    AddMonoApplications jane "/jane:/home/jane/web"

    MonoServerPath jane /nfs/mono-1.1.17/mod-mono-server

    <Location /jane>
        MonoSetServerAlias jane
        SetHandler mono
    </Location>

    Alias /jackie "/home/jackie/web"
    AddMonoApplications jackie "/jane:/home/jackie/web"

    # This uses mono from git and the ASP.NET 2.0 mod-mono-server
    MonoServerPath jackie /svn/install/bin/mod-mono-server2

    #
    # Add this directories to the default paths searched by mono
    # when looking for assemlies
    MonoPath jackie "/home/jackie/NET/assemblies:/usr/local/assemblies"
    # The .wapi directory will be created in /home/jackie
    MonoWapidir jackie "/home/jackie"
    <Location /jackie>
        MonoSetServerAlias jackie
        SetHandler mono
    </Location>

Troubleshooting

ASP.NET 2 applications do not work

If your site uses .NET 2.0 classes, you will need to instruct mod_mono to spawn the .NET 2.0 version of mod-mono-server, instead of the default .NET 1.1 version. To do that, use the following, changing the path to mod-mono-server2 and the alias as needed:

MonoServerPath default /usr/bin/mod-mono-server2

Access forbidden

If you’re getting a 403 response from apache that probably means that the user running apache does not have proper permissions to read the physical directory. Check the permissions on all the directories and the files and make then readable by the user running apache.

mod-mono-server does not start

Check the apache error_log file (/var/log/apache2/error_log …). It might contain some hints on what’s happening. Possible causes are that mono or mod-mono-server are not found in the path, that a file with the same name as the unix socket mod-mono-server tries to create already exists and mod-mono-server can’t remove it or a stale .wapi directory.

Restarting apache does not kill the spawned mod-mono-server.exe(s)

Use ‘apachectl reload’ instead of ‘apachectl restart’. There is some problem (may be fixed in apache 2.0.54) that made ‘restart’ not work properly.

For cookie-less sessions to work, you need to use SetHandler. AddHandler won’t work.

Under high load, mono process consumes a lot of memory, website stops responding

These symptoms have been reported, but their underlying causes are not known. Set the MonoAutoRestartMode, MonoAutoRestartRequests, MonoMaxActiveRequests, and MonoMaxWaitingRequests directives as described earlier to limit the lifetime of the mono process and to restrict the concurrency happening in the server.

mod_mono on Windows

For a Windows port of mod_mono, see here. This is a work in progress.

Profiling mod-mono-server

If you want to find the bottleneck in you ASP.NET application using mod_mono, and assuing you’re letting mod_mono start mod-mono-server, you’ll need to follow these steps:

  1. Start apache.
  2. Run ‘ps aux’ and copy the command line used to run mod-mono-server.exe
  3. Stop apache
  4. With the same user that runs apache, run the command line copied in 2 adding the ‘–profile’ parameter to mono.
  5. Start apache.
  6. Do a few requests (they will take a lot to process).
  7. Stop apache
  8. mod-mono-server will stop and you’ll get profile output.

Note that when –profile is enabled, mono is *extremely* slow. Do as many request as you need to get a result that excludes start up stuff.

Compiling mod_mono From Source

If you already have installed mod_mono as a package, skip this section.

Before compiling mod_mono, you need the development packages for apache installed (apache-dev…). You should have a file called mod_mono-X.Y.Z.tar.gz at this stage. Follow these steps:

    $ tar xzf mod_mono-X.Y.Z.tar.gz
    $ cd mod_mono-X.Y.Z
(1) $ ./configure
    $ make
(2) $ sudo make install

There are a few interesting options for (1) that you might want/need to use:

--prefix= /xxx/yyy
This will set the base path for installing mod_mono files.

--with-mono-prefix= /aaa/bbb
If the prefix for mod_mono is different from the one used for mono, you should set this to the prefix used for mono in order to make the default paths to mono executable and mod-mono-server.exe be correct. It is not mandatory, but useful.

--with-apxs= /xxx/yyy/apxs
If your system has different apache development files installed (ie, 1.3, 2.0 or 2.2) you might need this to choose the target version for mod_mono. Provide the full path to the apxs executable of the version that you want.

--with-apr-config= /xxx/yyy
If you get errors when compiling for apache 2 because some headers files are not found, use this option. It takes the full path to apache 2 apr-config tool.

--enable-debug
You will get more output in the apache error_log file. Useful when debugging.

Improving mod_mono

See our Improving mod_mono page for details on things that could be improved in the module.

mod_mono configuration examples

Introduction

mod_mono is an Apache 1.3/2.0 module that aims to provide ASP.NET support for the web’s favorite browser.

The module relays on an external program, mod-mono-server, to actually handle the requests. The communication between both is established using a unix socket or a TCP socket.

In the early days of mod_mono, you had to start mod-mono-server by yourself, ensuring that it had all the parameters needed to understand the requests forwarded by the module. This is still an option for those who want mod-mono-server to have a separate life cycle from apache, but you will probably prefer to use mod_mono built-in ability to start and stop mod-mono-server for you.

Requisites

You will need apache, the web server, installed.

From the Mono project ‘Downloads’ page you can get mono, XSP and mod_mono. XSP is the module where mod-mono-server resides.

Compiling mod_mono

If you already have installed mod_mono as a package, skip this section.

Before compiling mod_mono, you need the development packages for apache installed (apache-dev…). You should have a file called mod_mono-X.Y.Z.tar.gz at this stage. Follow these steps:

    $ tar xzf mod_mono-X.Y.Z.tar.gz
    $ cd mod_mono-X.Y.Z
(1) $ ./configure
    $ make
(2) $ make install

May be you need to run ‘su’ before (2).
There are a few interesting options for (1) that you might want/need to use:

  • –prefix=/xxx/yyy
    This will set the base path for installing mod_mono files.
  • –with-mono-prefix=/aaa/bbb
    If the prefix for mod_mono is different from the one used for mono, you should set this to the prefix used for mono in order to make the default paths to mono executable and mod-mono-server.exe be correct. It is not mandatory, but useful.
  • –with-apxs=/xxx/yyy/apxs
    If your system has different apache development files installed (ie, 1.3 and 2.0) you might need this to choose the target version for mod_mono. Provide the full path to the apxs executable of the version that you want.
  • –with-apr-config=/xxx/yyy
    If you get errors when compiling for apache 2 because some headers files are not found, use this option. It takes the full path to apache 2 apr-config tool.
  • –enable-debug
    You will get more output in the apache error_log file. Useful when debugging.

Configuration examples explained

Now that you have mono, XSP and mod_mono installed, you want some action.

Your first time

When you installed XSP, a bunch of sample ASP.NET pages and web services were installed too. If the prefix used to configure XSP was /usr, the sample files are located in /usr/share/doc/xsp/test.

Let’s say you want those file to be available under the virtual path /test. Edit your httpd.conf file (hint: /etc/httpd, /etc/apache2) and append the following lines (remove the numbers ;-):

1   LoadModule mono_module modules/mod_mono.so
2   Alias /test "/usr/share/doc/xsp/test"
3   AddMonoApplications default "/test:/usr/share/doc/xsp/test"
4   <Location /test>
5       SetHandler mono
6   </Location>

In line 1, you might need to use the full path to mod_mono.so.

Ok. Now start/restart apache and browse http://127.0.0.1/test/index.aspx. If you cannot see the test page, go to the troubleshooting section, otherwise, welcome to ASP.NET!

Now for the explanation of what the lines you added to httpd.conf do. Line 1 tells apache to load mod_mono module. I use line 2 always so that apache can check files and directories and appends a trailing slash if someone requests /test.

The first argument to AddMonoApplications in line 3 will be discussed in next section. The second one is passed to mod-mono-server so that it knows that the /test virtual path corresponds to the physical path /usr/share/doc/xsp/test. Then in line 5, inside our chosen location, we tell apache that mod_mono will handle all requests down /test/.

Okay, it worked. But, what happened? When you started apache, mod_mono launched mod-mono-server. Later, when you requested any page under /test, mod_mono connected to mod-mono-server, forwarded the request data and retrieved the response that is sent to your browser. Finally, if you stop apache, mod_mono will tell mod-mono-server to die.

More on applications

How do you go about making mod_mono handle several applications? Let’s try the easiest option. You will need this in your httpd.conf:

1   LoadModule mono_module modules/mod_mono.so
2   Alias /test "/usr/share/doc/xsp/test"
3   Alias /personal "/home/user/mypages"
4   AddMonoApplications default "/test:/usr/share/doc/xsp/test,/personal:/home/user/mypages"
5   <Location /test>
6       SetHandler mono
7   </Location>
8   <Location /personal>
9       SetHandler mono
10  </Location>

As you can see, it’s just a matter of adding the line 3 (like line 2) and lines 8, 9, 10 (similar to 5, 6, 7). The other change is in AddMonoApplications (4), whose value got the new application virtual and physical paths appended.

Now let’s focus on that line 4. Earlier in this document we said that mod_mono can run one or more instances of mod-mono-server and connects to it/them in order to dispatch ASP.NET requests. The first argument to AddMonoApplications and most of the other directives supported is an alias for a running mod-mono-server. Other directives allow omitting the first argument. In that case, ‘default’ is assumed. As you probably guessed:

AddMonoApplications default "/test:/usr/share/doc/xsp/test,/personal:/home/user/mypages"

	...is equivalent to...

AddMonoApplications default "/test:/usr/share/doc/xsp/test"
AddMonoApplications default "/personal:/home/user/mypages"

This way you can keep all the configuration related to an application in a separate file (Alias, AddMonoApplications, Location,…).

2 applications, 2 mod-mono-server

You might want to run different applications in different instances of mod-mono-server. The rationale for this can be that you don’t want Session or Application level objects to be shared among applications or that you want to set certain memory or CPU usage restrictions for an application or…

Let’s see the configuration needed to achieve this separation for the two applications in the previous example:

1   LoadModule mono_module modules/mod_mono.so
2   Alias /test "/usr/share/doc/xsp/test"
3   AddMonoApplications default "/test:/usr/share/doc/xsp/test"
4   <Location /test>
5       SetHandler mono
6   </Location>
7
8   Alias /personal "/home/user/mypages"
9   AddMonoApplications personal "/personal:/home/user/mypages"
11  <Location /personal>
12      MonoSetServerAlias personal
13      SetHandler mono
14  </Location>

With this configuration mod_mono will start two instances of mod-mono-server whose aliases are ‘default’ and ‘personal’. The ‘default’ instance will handle /test and the ‘personal’ instance will handle /personal. Every time mod_mono finds a new alias name in one of its directives, it will assign all the parameters for that alias to a new mod-mono-server instance.

As you can see there are two differences, apart from some lines shuffled. One is that AddMonoApplications in line 9 changed its first argument to be ‘personal’. The other is line 12, that introduces MonoSetServerAlias. This directive tells mod_mono which instance of mod-mono-server will be used to process the requests for this <Location>. It can be used with apache <Directory> too. Inside <Location /test> we didn’t need to use MonoSetServerAlias as the absence of this directive means that we want the ‘default’ instance to be used.

Setting limits

You know how to make mod_mono start one or more mod-mono-server instances. Here’s an example on how to set memory and CPU limits for a given server:

1   LoadModule mono_module modules/mod_mono.so
2   Alias /jeanette "/home/jeanette/web"
3   AddMonoApplications jeanette "/jeanette:/home/jeanette/web"
4   MonoMaxMemory jeanette 200000000
5   MonoMaxCPUTime jeanette 3600
6   <Location /jeanette>
7       MonoSetServerAlias jeanette
8       SetHandler mono
9   </Location>

Lines 4 and 5 set the maximum memory to be used (bytes) and the maximum CPU time consumed (seconds) by the ‘jeanette’ mod-mono-server instance. After reaching the limit, the OS will kill mod-mono-server. Upon next request mod-mono-server will be restarted by mod_mono with the same limits.

Unix and TCP sockets

We said that mod_mono and mod-mono-server can use a unix or a TCP socket to sent data back and forth. Use of unix sockets is the default, but you can use a TCP socket in case you have several computers running apache and one single machine providing mod-mono-server services.

The only parameter that you can control when using a unix socket is the file name. The directive is MonoUnixSocket:

    LoadModule mono_module modules/mod_mono.so

    Alias /julia "/home/julia/web"
    AddMonoApplications default "/julia:/home/julia/web"
    # When no MonoUnixSocket is provided, the default
    # is /tmp/mod_mono_server[_alias]
    # In this case, for the 'default' alias: /tmp/mod_mono_server
    <Location /julia>
        SetHandler mono
    </Location>

    Alias /jennie "/home/jennie/web"
    AddMonoApplications jennie "/jennie:/home/jennie/web"
    # In this case, alias 'jennie': /tmp/mod_mono_server_jennie
    <Location /jennie>
        MonoSetServerAlias jennie
        SetHandler mono
    </Location>

    Alias /juno "/home/juno/web"
    AddMonoApplications juno "/juno:/home/juno/web"
    # Uses a file under juno's home directory
-   MonoUnixSocket juno /home/juno/tmp/juno_server
    <Location /juno>
        MonoSetServerAlias juno
        SetHandler mono
    </Location>

You can set the file name to whatever you want as long as the user running apache has the necessary permissions to create and remove that file.

In order to run an instance of mod-mono-server that listens on a TCP socket, there’s a mandatory MonoListenPort directive and an optional MonoListenAddress. See them in action:

    LoadModule mono_module modules/mod_mono.so

    Alias /jazmin "/home/jazmin/web"
    AddMonoApplications jazmin "/jazmin:/home/jazmin/web"
    # 'jazmin' mod-mono-server will be listening on
    # port 9000, address 127.0.0.1
-   MonoListenPort jazmin 9000
    <Location /jazmin>
        MonoSetServerAlias jazmin
        SetHandler mono
    </Location>

    Alias /joan "/home/joan/web"
    AddMonoApplications joan "/joan:/home/joan/web"
    # 'joan' mod-mono-server will be listening on
    # port 7000, any address (0.0.0.0)
-   MonoListenPort joan 7000
-   MonoListenAddress joan 0.0.0.0
    <Location /joan>
        MonoSetServerAlias joan
        SetHandler mono
    </Location>

MonoUnixSocket and MonoListenPort are mutually exclusive.

Paths

In case it is needed, you can provide alternative locations for mono executable and mod-mono-server.exe. Other directories containing assemblies that mono could not find in the default search paths can also be specified. Mono needs a writable directory used by the windows I/O emulation layer that is usually in the user’s home .wapi directory ($HOME/.wapi). In mod_mono, the directory where .wapi is created is set to /tmp, but you can change that too.

    LoadModule mono_module modules/mod_mono.so

    Alias /jane "/home/jane/web"
    AddMonoApplications jane "/jane:/home/jane/web"
    # This uses mono 1.1.4 and ASP.NET 1.1 mod-mono-server
    MonoExecutablePath jane /nfs/mono-1.1.4/bin/mono
    MonoServerPath jane /nfs/mono-1.1.4/lib/mono/1.0/mod-mono-server.exe
    <Location /jane>
        MonoSetServerAlias jane
        SetHandler mono
    </Location>

    Alias /jackie "/home/jackie/web"
    AddMonoApplications jackie "/jane:/home/jackie/web"
    # This uses mono from SVN and the alpha ASP.NET 2.0 mod-mono-server
    MonoExecutablePath jackie /svn/install/bin/mono
    MonoServerPath jackie /svn/install/lib/mono/2.0/mod-mono-server2.exe
    # Add this directories to the default paths searched by mono
    # when looking for assemlies
    MonoPath jackie "/home/jackie/NET/assemblies:/usr/local/assemblies"
    # The .wapi directory will be created in /home/jackie
    MonoWapidir jackie "/home/jackie"
    <Location /jackie>
        MonoSetServerAlias jackie
        SetHandler mono
    </Location>

Troubleshooting

Access forbidden

If you’re getting a 403 response from apache that probably means that the user running apache does not have proper permissions to read the physical directory. Check the permissions on all the directories and the files and make then readable by the user running apache.

mod-mono-server does not start

Check the apache error_log file (/var/log/apache2/error_log …). It might contain some hints on what’s happening. Possible causes are that mono or mod-mono-server are not found in the path, that a file with the same name as the unix socket mod-mono-server tries to create already exists and mod-mono-server can’t remove it or a stale .wapi directory.

Introduction to Mono – Your first Mono app

The first in a series of articles about Mono. This article explains how to install Mono and shows how to compile your first Cross Platform application.

Introduction

Not so long ago the guys here at CP announced a new Cross Platform .NET section. At the time I was working completely with Windows based applications using C# and the .NET platform so, while the thought of getting to play around with C# apps on other platforms was interesting, it wasn’t something I had the time to do. Nevertheless, fate has once again stepped in, this time in the form of a new project, and soon I’ll be working on systems that will no doubt require some cross platform goodness. Since the majority of the development will still be for the Windows platform the technology set will be staying as is and the happiness that exists when developing .NET applications will remain.

Anyway, it dawned on me that I didn’t really know all that much about cross platform development with .NET. Sure, I had heard of Mono, Rotor and Platform.NET and had read some things here and there but I didn’t have any practical knowledge about using any of these. After a bit of research I decided that Mono was the most promising choice for the upcoming project and I decided to give it a go. I am fairly familiar with Linux and have developed applications, albeit basic ones, for Linux using C, C++ and Java when in college. However, many weblog postings from bemused Windows developers who had attempted to get Mono up and running on Linux had me slightly worried before I began. Much to my surprise, it was actually a painless experience and getting Mono running on Linux was easy. Setting it up and running it on Windows was even easier again.

What exactly is Mono?

When Microsoft unleashed .NET onto the world they were nice enough to hand the Common Language Infrastructure (CLI), along with the C# language, to the people at the European Computer Manufactures Association, better known to most as the ECMA. This allowed for an ECMA standards for the CLI and C# to be developed and, eventually, these ECMA standards also became ISO standards. At this stage you might be thinking “So, what has this got to do with what Mono is?” Relax, slow down, I’m getting to it. This standardization meant that details about how a CLI implementation should work, and also details about the C# programming language, where publicly available and anybody with the time and desire could implement their own CLI, essentially making their own version of .NET. Mono was born.

Mono is an “open source development platform based on the .NET framework that allows developers to build Linux and cross-platform applications with unprecedented productivity”. At the time of writing this article the latest stable version of Mono is 1.0.5 and it provides the following features:

  • A Common Language Runtime (CLR) that is compatible with the ECMA standard
  • A C# compiler
  • A set of class libraries
  • Ancillary tools such as a disassembler, debugger, IDE, etc.

Although it may not sound like much to many, what the people behind the Mono project have achieved is quite impressive. The set of class libraries available with mono include implementations of ADO.NET, ASP.NET, and System.Windows.Forms as well as many other aspects of the BCL that .NET developers will be familiar with. Added to this is the Gtk# library which is a fully featured library that allows for the development of GUI apps on top of the gtk+ toolkit. All in all, quite impressive.

With Mono you can develop and run .NET applications on Linux, Windows and other platforms. You can build and deploy ASP.NET application on Linux servers. You can even run applications that you compiled using the Microsoft C# compiler on the Mono runtime and vice versa.

What is the purpose of this article?

This article is the first in a series of articles that I will be writing about developing cross platform applications using Mono. Its purpose is to explain how to get up and running with Mono on Linux and on Windows. I’ll explain how you install Mono and then I’ll show you how to use the C# compiler to compile and run what might be your first cross platform .NET application. I’ll also list some of the ancillary tools that come with Mono. Finally, I’ll show how you can run applications created using Mono on the Microsoft .NET Runtime and vice versa.

Unfortunately I haven’t had a chance to play with Mono on a Mac so I won’t be covering it in this article. However I will try to cover it, along with Platform.NET, in a future article. No promises though.

What you need to know

This article is not going to show you how to program in C#. This is something that you will have to learn yourself. There are many good introductory articles here on CP that you can read to gain a firm understanding of the ins and outs of the language.

An understanding of what the .NET Framework and what it is is not necessary, but it would definitely be beneficial.

Lets get going

Before you can develop anything using Mono you first need to get it up and running on your desired platform. Since this article is in the Cross Platform .NET section and one of the main uses of Mono is the development and use of cross platform applications, I cover installing Mono on both Windows and Linux. I’ll look at installing on Windows first even though, to be perfectly honest, it’s a bit of a no brainer. As I mentioned above, the latest stable version of Mono at the time of writing this article is 1.0.5 and I will be working with this version. The download page for the latest Mono releases is http://www.mono-project.com/downloads/index.html[^] but I will be giving direct links to the files I download in each of the sections.

If you would prefer to skip installing Mono on Windows then you can go straight to the Installing Mono on Linux section below.

Installing Mono on Windows

The Windows version of Mono runs on Windows 2000 and above. I’m working on Windows XP Professional with Service Pack 2 installed. It probably doesn’t make a difference but I just wanted to mention it in case my instructions on installing Mono on Windows differs slightly from the procedure you need to follow for your own setup. Just follow these steps and you should be up and running in no time:

  1. Download the Mono Windows Installer from the Mono download page. The file I am working off is http://www.go-mono.com/archive/1.0.5/windows/mono-1.0.5-gtksharp-1.0.5-win32-0.1.exe
  2. The installer is a standard Windows installer so you can simply run it once it is downloaded by doubling clicking on it.
  3. Once the installer begins, click on next to go to the License Agreement page.
  4. You’ll probably notice on the License Agreement page that there are a number of different Licenses that apply to the different components of the install. While I understand that most people simply agree and click on next, it would be irresponsible of me if I didn’t recommend that you read the License Agreement before agreeing to it.
  5. Once you pass the License Agreement section you are presented with some general information about the install. The installer that I link to above mentions that it includes Gtk# and XSP (A Mono based web server that I will be looking at in the next article in this series) among other things.
  6. Click on Next and select the install location.
  7. Click on Next again to bring you to the Select Components page. You are free to customize your installation but as I will be using all the components of this install in other articles in this series I would suggest that you accept the default “Full installation”.
  8. Click on Next again and you can customize the name of Mono folder that appears in the Start Menu.
  9. If you have decided to do a Full installation or if you have installed XSP, the installer will now ask you which port you want the web server to listen on. Generally this will be port 8088 but you can change it to any free port on your system. When you are ready click on Next.
  10. Finally, click on Install to install and configure Mono on your Windows machine.

The above list of instructions is probably too verbose. Most people would have been fine just clicking on Next the whole way through. However, I normally take my time when installing things. You never know what people slip into an installer and it’s always good to know exactly what you have put on your machine.

At this stage you can keep reading and learn how to install Mono on your Linux machine or if you don’t have a Linux installation, or just want to develop on Windows for the time being, you can skip ahead to the Your Mono toolset section.

Installing Mono on Linux

Linux is a great operating system. There is no doubting that. However, there are some basic problems with the Linux platform that have stemmed from the fact that the kernel and most of the supporting software is open source. The problem is a simple one; there are so many different Linux distributions that there is no single set way of installing software on them. Why should you care about this? Well, depending on the distribution that you use, it could end up making your life a little more complicated then it needs to be when installing Mono. You’ll see what I mean below.

To get Mono up and running I decided to go for one of the more popular Linux distributions. Partly because I was familiar with it and partly because there was already an iso of the installation DVD on one of the internal FTP servers in work. No point in downloading a different distribution if I have a perfectly good one here. So, what did I choose? SuSE 9.2 Professional. I used Microsoft Virtual PC 2004 to create two identical virtual PCs. Two because I wanted to try two different ways of installing and configuring Mono that were not mutually exclusive. I didn’t want them to interfere so I decided that two separate virtual PCs was the way to go. I mounted the installation DVD iso in the virtual PCs and went about installing an identical system on both virtual PCs. I chose to install with ACPI disabled, which is one of the first options you get to choose, and I went for the default package selection. Nothing fancy. Just the basic, default system that SuSE 9.2 Professional suggested.

If you have a different Linux distribution (have a look at http://www.distrowatch.com/ for a good list of the different distributions available) then chances are that the installation instructions below will differ slightly for you.

The easy way

As the name suggests, this way is, well, easy. The problem with this way however, is that it will not work on all Linux distributions. If you have a look at the Mono download page (http://www.mono-project.com/downloads/index.html[^]) you will see that there are a packages available for some versions of the more common Linux distributions including Red Hat, SuSE, Fedora, and Novell Linux Desktop. Now a lot of Linux distributions will be compatible with some of these installation packages but specifying which ones are and which ones are not is well outside the scope of this introductory article. Unfortunately, if you are not using one of the listed distributions you’ll have to either find out from your distributions documentation which of the packages are compatibly or you can download the Mono source and build Mono from that (covered in the Not for the faint hearted section below). If you are not familiar with Linux I would recommend trying your best to avoid the latter option as it can get messy.

So, you have a Linux distribution that the Mono download page seems to like. Great. You’re in luck as this is going to be painless:

  1. On the Mono download page, click on the “Packages” link beside the Linux distribution that you believe to be compatible with your distribution. I simply selected the link beside SuSE 9.2
  2. This should bring you to a page which lists a lot of different packages that have been compiled and packaged for that particular Linux distribution.
  3. For the time being we are only interested in the “Mono Core Runtime and C# compiler” section in the top left.
  4. In this section you need to click on the download link for the Mono Core. The exact name will vary depending on the packages link that you selected but in general it will start with “mono-core-1.0.5”. I used http://www.go-mono.com/archive/1.0.5/suse-92-i586/mono-core-1.0.5-1.ximian.10.4.i586.rpm
  5. Download this file and remember where you have saved it!
  6. When it has downloaded simply open the file and it will launch the package manager for your particular distribution.
  7. From this point on you’ll have to follow the particular installation procedure for your package manager but in general there is simply an “Install Package” button. Click it!
  8. After a while, the Mono core should be installed. Congratulations

If you are inquisitive, bored, or if the above instructions didn’t work for your distribution then the next section runs over how to build and install from the source. In general this should be a painless procedure but as you will see, it turned out to be less then painless with the default SuSE 9.2 Professional install that I decided to work from.

When you went to the packages page you probably noticed downloads for Gtk#, XSP and a number of other things that the Windows installer installed by default. For the time being you can ignore these. In future articles I will need you to have these packages installed but if and when the need arises, I will run through the installation process.

Not for the faint hearted

If you have Mono installed on your Linux distribution at this stage then you can skip ahead to the Your Mono toolset section. That is, of course, unless you are interested in finding out how I spent two hours getting the source to compile.

Ok. So you want to compile Mono from the source and then install it. No problem. This “should” be a simple process. I have configured, compiled and installed applications from the source code numerous times on Linux without problem. Not this time. In this section I’m going to explain to you how things “should” work from the source. I’ll then show you how things worked for me when I configured, compiled and installed from Mono Source. Here goes:

  1. The first thing you need to do is download the source. Once again, head on over to the Mono download page at http://www.mono-project.com/downloads/index.html[^].
  2. In the source code section click on and download the Mono Runtime source (usually at the very top). The one I downloaded and worked off is http://www.go-mono.com/archive/1.0.5/mono-1.0.5.tar.gz.
  3. Once the file has been downloaded, go to a console for the remainder of the process.
  4. I would recommend performing the rest of the process as a user with root privileges. If you have just installed Linux as a standalone machine you can usually give your account root privileges by typing the sudo bash command and entering the root password.
  5. You can decompress the downloaded file using the command tar -xvzf mono-1.0.5.tar.gz where mono-1.0.5.tar.gz is the name of the file you downloaded.
  6. Next, switch to the directory where the source code has been decompressed too by typing cd mono-1.0.5 or cd followed by the name of the directory that you decompressed the file to if it is different from mine.
  7. Now, before we can compile the source you need to configure the make files. To do this simply type ./configure --prefix=/usr. This will take a bit of time. I am assuming here that there were no errors and that the ./configure --prefix=/usr went ahead without issue. As you will see from my experience, which I outline below, this was not the case for the default SuSE 9.2 Professional installation.
  8. When the the ./configure --prefix=/usr  completes you are free to go ahead and compile and install the source.
  9. To actually compile the Mono Runtime source you need to run the make command.
  10. Finally, to install, for lack of a better word, simply run the make install command. This will deploy the compiled binaries, libraries and other bits and pieces to the correct directories.

Now, that is how things should work. And, if I had installed a version of SuSE 9.2 Professional with the development tools installed, then that is what would have happened. However, as I have mentioned before, I opted for the default installation. I wanted to see how things would be for somebody who was coming from Windows and was more accustomed to OS installs that require the minimum of configuration.

The following, rather long set of steps, is what I had to do to configure, compile and install the Mono Runtime source. It not that it is a complicated process. Everything is actually quite simple. It’s just that it can be rather daunting to someone with very little Linux experience to see their initial ./configure --prefix=/usr fail when all the documentation for the Mono install tells them to do is type three commands: ./configure --prefix=/usr then make and finally make install. I was in two minds about whether I should add my experience with the Mono source as it is quite long and it might not add anything to the article. However, since I was having these issues I was sure that others would too and hopefully my experience can help you if you are faced with similar problems. Feel free to skip the rest of this section if you already have Mono installed.

So. Here’s what I done, and all just for you!:

  • Downloaded “Mono Runtime 1.0.5” from http://www.go-mono.com/archive/1.0.5/mono-1.0.5.tar.gz
  • Decompressed the download and then moved into the source directory.
  • Ran the sudo bash command to give myself root privileges.
  • Ran ./configure --prefix=/usr for Mono.
    • ./configure --prefix=/usr failed as default SuSE install does not have the needed compilers installed.
    • Installed Compilers (gcc c++ and it’s dependencies) using Yast.
  • Ran ./configure --prefix=/usr for Mono again.
    • ./configure failed because the default install does not have bison installed.
    • Downloaded bison from http://ftp.gnu.org/gnu/bison/bison-2.0.tar.gz.
    • Decompressed bison download and moved into the decompressed directory.
    • Ran ./configure for bison.
      • ./configure for bison failed because GNU m4 version 1.4 (or greater) is needed.
      • Downloaded m4 from http://ftp.gnu.org/gnu/m4/m4-1.4.1.tar.gz.
      • Decompressed m4 download and moved into the decompressed directory.
      • Ran ./configure for m4… Succeeded!
      • Ran make for m4… Succeeded!
      • Ran make install for m4… Succeeded!
    • Ran ./configure for bison, again… Succeeded!
    • Ran make for bison… Succeeded!
    • Ran make install for bison… Succeeded!
  • Ran ./configure --prefix=/usr for Mono for the third time.
    • ./configure failed, again, because “pkg-config” is needed and is not installed by the default SuSE 9.2 install.
    • Downloaded pkg-config from http://www.freedesktop.org/software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz.
    • Decompressed pkg-config download and moved into the decompressed directory.
    • Ran ./configure for pkg-config… Succeeded!
    • Ran make for pkg-config… Succeeded!
    • Ran make install for pkg-config… Succeeded!
  • Ran ./configure --prefix=/usr for Mono for the fourth time!
    • Guess what?.. Yep.. ./configure failed again. This time because glib-2.0 (or greater) was needed and was not installed by the default SuSE 9.2 install.
    • Downloaded glib-2.6.1 from ftp://ftp.gtk.org/pub/gtk/v2.6/glib-2.6.1.tar.gz.
    • Decompressed glib-2.6.1 download and moved into the decompressed directory.
    • Ran ./configure for glib-2.6.1
      • ./configure for glib-2.6.1 failed because there was no “gettext” support in the C libraries installed in the system
      • Downloaded gettext 0.14 from ftp://ftp.heanet.ie/mirrors/ftp.gnu.org/gnu/gnu/gettext/gettext-0.14.tar.gz.
      • Decompressed gettext 0.14 download and moved into the decompressed directory.
      • Ran ./configure for gettext… Succeeded!
      • Ran make for gettext…
        • make for gettext failed because there is not C# compiler installed! WTF | :WTF: Confused | :confused:make suggests installing Platform.NET Roll eyes | :rolleyes:
        • Reconfigured gettext as there was no way I was installing Platform.NET just so I could install Mono.
        • Ran ./configure --disable-csharp for gettext… Succeeded!
      • Ran make for gettext, again… Succeeded!
      • Ran make install for gettext… Succeeded!
    • Ran ./configure for glib-2.6.1 again… Succeeded!
    • Ran make for glib-2.6.1… Succeeded!
    • Ran make install for glib-2.6.1… Succeeded!
  • Ran ./configure --prefix=/usr for Mono for the fifth time… Succeeded! Finally!
  • Ran make for Mono… Succeeded!
  • Ran make install for Mono… Succeeded!
  • Took a well earned breather.

The above set of steps that I had to go through to get the source compiled might seem a little daunting to some but to be perfectly honest it was an easy enough procedure. The main issue was the time taken. For example, the ./configure for Mono could take around 2 or 3 minutes each time. Each ./configure for the other items that had to be compiled and installed had similar time periods and generally speaking the make command took longer than its accompanying ./configure. There was also the time spent looking for, downloading and decompressing the source files for the different items that needed to be installed. Oh, and I had to actually restart SuSE before I could use the installed tools.

Your Mono toolset

At this stage you’ve either given up or you’re in for the long haul. Not that there is much left to do. We have Mono installed and the tools that we need to compile and run C# applications are ready to be used. Before we start to actually write and compile applications I thought it might be a good idea to have a quick overview of some of the tools the you should have available to you at this stage. I’m not going to go into any detail on the tools as in this article, and over the rest of this series, I will explain the details of the tools as they are needed.

Tool Description
disco Mono’s Web Service Discovery Tool
A tool for discovering web services and for retrieving the documents that describe those services.
gacutil Global Assembly Cache management utility
A tool used by developers to install assemblies into the system Global Assembly Cache (GAC) to become part of the assemblies that are available for all applications at runtime.
ilasm Mono IL assembler
The Mono IL assembler. Comparable to the Microsoft ilasm.exe tool.
mcs Mono Compiler Suite.
The Mono C# compiler, an implementation of the ECMA-334 language specification. The compiler accepts all the same command line options that the Microsoft C# compiler (csc.exe) does.
mint Mono Interpreter
A mono interpreter that allows for the execution of applications without using JIT. The instructions are interpreted directly into x86 instructions.
mono Mono’s ECMA-CLI native code generator (Just-in-Time and Ahead-of-Time)
A runtime implementation of the ECMA Common Language Infrastructure. This can be used to run ECMA and .NET applications.
monodis Mono assembly disassembler
A tool that allows applications to be disassembled into IL. Provides similar functionality to Microsoft’s ildasm.exe.
monograph Mono assembly information tool
A tool that allows you to create call graph or type hierarchy information from assemblies.
monop Mono Class Outline Viewer
A tool that allows you to view the outline of a class. You can see the signature of each member of the class.
sn Digitally sign/verify/compare strongname on CLR assemblies
Digitally sign, verify or compare, CLR assemblies using strongnames.
soapsuds Mono’s Remoting Proxy Generator
A tool for generating WSDL documents and client proxies for remoting services.
wsdl Mono’s Web Service Proxy Generator
A tool for generating proxy classes that can be used to access to web services.
xsd Mono’s utility for generating schema or class files
A tool that is intended to complement the XML serialization support of Mono.

Remember, this is only a subset of the tools that are available. If you are interested in exactly what tools are available you should have a look on the Mono homepage.

Also, when using Mono on Windows it would be a good idea to add the <Mono_Install_Dir>\bin\ and <Mono_Install_Dir>\lib\ directories to your path (C:\Program Files\Mono-1.0.5\bin\ and C:\Program Files\Mono-1.0.5\lib\ on my system). This will allow you to use the above tools directly from the command line regardless of which directory you are currently working in.

Let’s code!

About time! To get to this point should not have taken you all that long, unless of course you ended up installing Mono on Linux from source and ran into the trouble that I did. So let’s get on with it.

Hello World!

The proverbial “Hello World!”. You’ve no doubt seen a hello world application in C#, or other languages, numerous times in the past and you are once again going to see it. It’s kind of a tradition and who am I go against tradition?

Anyway, fire up your favorite text editor, be it notepad on windows or joe on Linux. Type in the code below, or even easier, copy it from the article and paste it in.

using System;

namespace Dela.Mono.Examples
{
   public class HelloWorld
   {
      public static void Main(string[] args)
      {
         Console.WriteLine("Hello World");
      }
   } 
}

Save this as HelloWorld.cs and get ready to witness the wonders of Mono. To compile this application simply go to the command line and type mcs HelloWorld.cs and hit enter. Note that the previous command assumes that your command prompt is working in the same directory as the HelloWorld.cs file and that the Mono directories have been added to your path if you are using Windows. If all goes well you should be presented with the “Compilation succeeded” text. Congratulations. You’ve just compiled your first C# application on Mono.

To execute the application using Mono simply type mono HelloWorld.exe and hit enter. You’ll be greeted by the text “Hello World”. What you have done here is loaded the Mono JIT Runtime which JIT compiled the IL code inside the HelloWorld.exe assembly and executed the instructions. You could have also executed the application by running mint HelloWorld.exe. This would have used the Mono Interpreter to convert all the IL of the application directly to x86 instructions before executing. This is similar to using the ngen.exe application with .NET.

If you are running Mono on Windows you may have noticed that you can also execute the application by simply double clicking on it. You may not see this as being special in any way but what you have done is taken an application created with the Mono C# compiler and executing it on the Microsoft .NET Runtime. More about this below in the .NET and Mono interoperability section.

Something a little more complicated

Hello World is the traditional first program, but in all honesty it doesn’t really do much. The next example is a bit contrived but it shows a few more of the features of Mono including the System.Net, System.Text, and System.Text.RegularExpressions namespaces. I also know that the code is all lumped into the Main method and that it should be refactored but as I said, this is just a contrived example, and not a lesson in code style.

The example simply takes a string that you want to search on http://www.google.com/ for and then displays the first five results that comes back. Nothing major but it does show an ever so slightly more complicated example than the Hello World example above. Anyway, once again, fire up your favorite text editor, type in the code below or copy it from the article and paste it in.

using System;
using System.Net;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;

namespace Dela.Mono.Examples
{
   class GoogleSearch
   {
      static void Main(string[] args)
      {
         Console.Write("Please enter string to search google for: ");
         string searchString = HttpUtility.UrlEncode(Console.ReadLine());
         
         Console.WriteLine();
         Console.Write("Please wait...\r");

         // Query google.
         WebClient webClient = new WebClient();
         byte[] response =
              webClient.DownloadData("http://www.google.com/search?&num=5&q="
              + searchString);

         // Check response for results
         string regex = "g><a\\shref=\"?(?<URL>[^\">]*)[^>]*>(?<Name>[^<]*)";
         MatchCollection matches
                  = Regex.Matches(Encoding.ASCII.GetString(response), regex);

         // Output results
         Console.WriteLine("===== Results =====");
         if(matches.Count > 0)
         {
            foreach(Match match in matches)
            {
               Console.WriteLine(HttpUtility.HtmlDecode(
                  match.Groups["Name"].Value) + 
                  " - " + match.Groups["URL"].Value);
            }
         }
         else
         {
            Console.WriteLine("0 results found");
         }
      }
   }
}

Save this file as GoogleExample.cs and go back to the command line. The command to compile this application is a little different to the one we used for the Hello World example. This application makes use of the HttpUtility class which is defined in the System.Web.dll assembly. That means we need to reference this assembly when compiling. To do this you simply use the -r switch. This is the full command you need to type to get this application to compile: mcs GoogleExample.cs -r System.Web.dll
Once again, the previous command assumes that your command prompt is working in the same directory as the GoogleExample.cs file and that the Mono directories have been added to your path if you are using Windows.

As with the Hello World example you can run this application from the command line using mono GoogleExample.exe or mint GoogleExample.exe – Take your pick. The same compiled assembly will run on Windows and Linux. You can test this by compiling it on Windows, copying the assembly to a Linux machine and running the mono GoogleExample.exe command there.

.NET and Mono interoperability

I mentioned in the Hello World example above that you can simply take the assembly created by the Mono compiler (HelloWorld.exe in that example), double click on it in a Windows environment with .NET installed, and it would execute. How exactly does that work? Well, it’s simple really. The Mono C# compiler, and the Microsoft C# compiler, compile code into an intermediary form known as IL. When you execute an assembly the runtime interprets and executes the IL code. So you can take an assembly compiled from VB.NET code, or any .NET language, and run it on Mono and vice versa. There is however one small issue with this; Mono doesn’t have a complete implementation of the .NET class libraries so not all of your applications can be run using Mono. Don’t worry too much about that now as over the next few articles in this series we’ll see what can and can’t run and hopefully examine why.

What next?

Hopefully this article has shown you how to install Mono and get your first Mono applications up and running on Windows and on Linux. That’s not all that much though. You’ll obviously want to do more than just create simple console applications. As I’ve said throughout this article, this is just the first in a series of articles that I’ll be writing about Mono and Cross Platform .NET development using Mono. So, what’s on the cards for the next article? Well, I know it would probably be best to jump in a start creating cross platform GUI apps but before we look at this I want to do an introduction to Web Application and ASP.NET development using Mono, which is what the next article will be about; writing your first ASP.NET application that runs on both Linux and Windows.

Links

History

June 8th, 2005 – Corrected details about Mono tool “mint”.

March 1st, 2005 – Updated some language mistakes and changed to install Mono in the /usr directory when installing from source on Linux.

January 25th, 2005 – First Posted.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s