Using Reactive Extensions with Mono

November 21, 2012 at 8:44 pm | Posted in C#, Programming | 3 Comments

I first learned about Reactive Extensions (Rx) begin this month when it was open sourced by Microsoft. Although I found a few scattered references on the internet on how to get Rx working with Mono, I had to jump through quite a few hoops. This blogpost is a detailled account and will hopefully save you a couple of hours.

Getting Reactive Extensions

When you are using Windows this is pretty straightforward. But then again, in that case you are probably using .NET and not reading this blogpost at all. However when you are using Linux or OS-X it gets a bit more complicated. In that case your only option is to use NuGet.

Getting NuGet

I didn’t download the recommended version (NuGet.exe Bootstrapper 2.0) but used the NuGet.exe Command Line. This didn’t work out of thebox. According to this excellent blog post you first have to import some root certificates so that Mono will trust NuGet:

$ mozroots --import --sync

Next you type:

$ mono NuGet.exe

This will result in output similar to:

NuGet bootstrapper 1.0.0.0
Found NuGet.exe version 2.1.2.
Downloading…
Update complete.

You now have NuGet running. To get help type:

$ mono NuGet.exe help

Getting Rx-Main

Ok, so let’s finally get Rx. I started with the latest and greatest (Rx-Main 2.0.21114 at the moment of writing) but I didn’t get that working. However version Rx-Main 1.0.11226 does seem to work with Mono. To see all available versions enter:

$ mono NuGet.exe list Rx-Main -AllVersions

To install the latest Rx 1.0 enter:

$ mono NuGet.exe install Rx-Main -Version 1.0.11226

This will download Rx-Main into your current working directory. You can find the dll you need as: ./Rx-Main.1.0.11226/lib/Net4/System.Reactive.dll

Compiling your first Rx program

With the downloaded dll we can finally build our first Rx program. As an illustration (you can find more examples and explanation on the Reactive Framework Wiki) I used the following code:

using System;
using System.Reactive;
using System.Reactive.Linq;

class Rx
{
  public static void Main(string[] args)
  {
    var input = Observable.Range(1, 15);

    input.Subscribe(x => Console.WriteLine("The number is {0}", x));
  }
}

If you save this code as rx.cs you are ready to compile your first Rx program. Make sure that you have the System.Reactive.dll in the same directory or set the library path for the Mono compiler using the -lib directive. Assuming the dll is in the same directory as your source, just type:

$ mcs -r:System.Reactive rx.cs

This will create a rx.exe that can of course be executed with:

$ mono rx.exe

Next steps

This is all you need to get Rx and Mono working. I tried with both Mono 2.10.x and 3.0.x on OS-X and Linux. As mentioned before, I only got this running with Rx 1.0.x which uses a single dll. In Rx 2.0.x this dll is split-up into several dll’s. However trying to compile this leads to:

Unhandled Exception:
IKVM.Reflection.MissingMemberException: Member ‘System.IComparable`1′ is a missing member and does not support the requested operation.

I haven’t investigated this any further yet, but it might very well be a Mono versus .NET incompatibility.

Have fun hacking Rx and Mono and please let me know if you have any questions or remarks.

About these ads

3 Comments »

RSS feed for comments on this post. TrackBack URI

  1. If you compile mono from master, you got ReactiveExtensions bundled, no need to do any weird things to install them.

    • Good to hear. Thanks for mentioning!

  2. These are truly wonderful ideas in concerning blogging. You have touched some
    nice factors here. Any way keep uup wrinting.


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

Blog at WordPress.com. | The Pool Theme.
Entries and comments feeds.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: