What Is Mono?
Mono (http://www.mono-project.com/) is a run time along the same lines as the .NET Framework, and it includes much of the functionality of the .NET Framework. In fact, with each release, Mono gets a bit closer to .NET Framework functionality. However, don’t get the idea that Mono will ever exactly match the .NET Framework. Platform differences, Microsoft copyrights, and other issues will always keep Mono just a bit different from the .NET Framework. Even so, Mono can run a considerable number of .NET applications. The following sections describe Mono, its advantages and limitations, in greater detail.
An Overview of the Mono Family
You can obtain Mono for a considerable number of platforms. In fact, the makers of Mono add new platforms with every release. At one time, Mono worked on just a few Linux implementations, Windows, and the Mac OS X. Over time, Mono support has increased to the exciting list of platforms that follows.
- LiveCD: This is actually an openSUSE 11.2.1 (http://www.opensuse.org/en/) LiveCD (a CD or DVD that contains a bootable image — see http://en.wikipedia.org/wiki/ Live_CD for details) that includes Mono 2.6.1.
- Mac OS X: You can use this installation on a number of Mac versions including Mac OS X Tiger (10.4), Leopard (10.5), and Snow Leopard (10.6) (it may work on other versions as well, but you’re on your own for support). The download includes Mono, Cocoa#, and Gtk# (GIMP Toolkit Sharp). You need to download the Client Software Development Kit (CSDK), available on the Mono site, separately. There are separate downloads for the Intel and PowerPC platforms. You can learn more about Mac OS X at http://www.apple.com/macosx/.
- openSUSE: You can use this download for the openSUSE 11.0, 11.1, and 11.2 platforms. You must have your own system with openSUSE installed to use it. You can download openSUSE at http://software.opensuse.org/. Just in case you’re interested, the SUSE part of the name stands for Software und System-Entwicklung, which translates to software and systems development.
- SLES/SLED: You can use this download for SUSE Linux Enterprise Server (SLES) or SUSE Linux Enterprise Desktop (SLED). SLES and SLED are the paid versions of SUSE from Novell. As with openSUSE, you must have your own system with SLES or SLED installed to use this version of Mono. You can find out more about SLES and SLED at http://www.novell.com/linux/.
- Virtual PC: This is actually an openSUSE 11.2.1 virtual PC image that includes Mono 2.6.1. You could use this download to check out Linux functionality for your IronPython application on your PC without leaving Windows. Of course, performance won’t be very good, but it will get the job done.
- VMware: This is actually an openSUSE 11.2.1 VMware image that includes Mono 2.6.1. You’d use it to check your application for Linux functionality without leaving the host operating system.
- Windows: You can officially use this download for Windows 2000, XP, 2003, and Vista. Testing shows that it also works fine for Windows 7 and Windows Server 2008. The download includes Mono for Windows, Gtk# (a graphics library to display a user interface onscreen), and XSP (eXtensible Server Pages, an alternate Web server for serving ASP.NET pages). You can also get the Mono Migration Analyzer tool as a separate download.
- Other: This is a group of less supported platforms including Debian and Ubuntu. At least these two platforms have supported packages. You can also get Mono in an unsupported form for Solaris, Nokia, and Maemo. Theoretically, you could support yet other platforms by compiling the source code found at http://ftp.novell.com/pub/mono/sources-stable/.
Of course, this list contains only a summary of the main Mono downloads. There are a large number of Mono add-ons a well. For example, you can obtain Mono Tools for Visual Studio (http:// go-mono.com/monotools/download/) if you want to work with Mono directly from Visual Studio. Unfortunately, the current version of this product only works with Visual Studio 2008. The developer should provide a Visual Studio 2010 version soon. You can obtain a trial version of Mono Tools for Visual Studio (registration is required), but you must pay for the full version.
IronPython does include support for Silverlight development. If you plan to use IronPython for Web applications and need to support multiple platforms, you might want to look at Moonlight (http:// mono-project.com/Moonlight) instead. This Silverlight replacement works on the same platforms that Mono does and should also work fine with IronPython.
Some of the extensions to Mono are well outside the scope of this book, but are interesting to contemplate. For example, you can get Mono Touch (http://monotouch.net/) to develop applications for the iPhone and iPod Touch devices. The point is that you can probably find some form of Mono to meet just about any need, but using Mono fully means learning some new techniques, such as creating user interfaces using Gtk#.
Considering the Reasons for Using Mono
You already know the reasons that you’re using the .NET Framework and this chapter isn’t about changing your mind. The .NET Framework is stable and many developers love the functionality it provides them for building great applications. However, you could think of Mono as another tool to extend the range of your applications. If for no other reason, the fact that you could run your IronPython application on Linux or the Mac OS X makes Mono a good choice for some forms of application development. In sum, the main reason for using Mono in place of the .NET Framework is flexibility.
As previously mentioned, Mono and the .NET Framework aren’t precisely the same. The first thought that most developers will have is that compatibility issues will be bad, and to a certain extent, they do cause problems. However, Mono also provides functionality that you won’t find when working with the .NET Framework. Features such as Gtk# actually make Mono a better product. In addition, with Mono you have a lightweight Web server for ASP.NET pages, XSP, that works on every Mono platform. Therefore, the differences between Mono and the .NET Framework aren’t always bad — sometimes they become downright useful.
Mono does provide direct support for IronPython, but you need to use a newer version of Mono (see http://www.mono-project.com/Python for details). The support isn’t all that good. The section “Running the Application from the Command Line” later in this chapter demonstrates the problem of using the Mono implementation of IronPython. Even so, you do get IronPython support that will likely improve as Mono improves, so this is an area where you can expect Mono to grow as an IronPython platform. In reality, the Mono community is quite excited about IronPython. You can find tutorials for using IronPython in a Mono environment at http://zetcode.com/tutorials/ ironpythontutorial/. If you want to see IronPython running under Mono on a Linux system, see the screenshot and description at http://www.ironpython.info/index.php/Mono.
Understanding Mono Limitations
Don’t get the idea that every .NET application will instantly run on Mono. For example, while Mono includes support for Language Integrated Query (LINQ), the support isn’t perfect. The LINQ to SQL support works fine for many applications, but not all of them. The Mono developers realize that the support isn’t complete and they plan to work on it (see the release notes at http://www.mono-project .com/Release_Notes_Mono_2.6.1 for details).
There are some obvious limitations for using Mono that should come to mind immediately. Because the purpose of Mono is to work across platforms, the P/Invoke calls in your extensions aren’t going to work. A P/Invoke call causes your extension to provide Windows-specific support, so using it on Linux wouldn’t work no matter what product you tried. The previous chapters in the book have emphasized when a particular technique is unlikely to produce useful cross-platform results.
The current version of Mono doesn’t work with .NET Framework 4.0 applications. The applications won’t start at all — you see an error message instead. However, Mono does work fine with older versions of the .NET Framework. It’s only a matter of time before Mono supports the .NET Framework 4.0, so this is a short-term limitation that you can easily overcome by using an older version of the .NET Framework when building your application. Given that IronPython doesn’t currently support the .NET Framework 4.0 in many respects, this particular problem isn’t much of an issue for IronPython developers.
In a few cases, you have to look around to determine whether you’ll encounter problems using Mono for a particular task. For example, if your ASP.NET application uses Web Parts, you can’t use Mono (see http://www.mono-project.com/ASP.NET). You also can’t use a precompiled updateable Web site.
Using Mono on Windows Server 2008 Server Core
Early versions of Windows Server 2008 Server Core (Server Core for short) don’t come with any form of the .NET Framework. Consequently, you can’t run any form of .NET application on early versions of Server Core unless you use Mono. The lack of .NET Framework support on Server Core led some people to come up with odd solutions to problems, such as running PowerShell (see the solution at http://dmitrysotnikov.wordpress.com/2008/05/15/powershell-on-server-core/).
Fortunately, Microsoft decided to provide a limited version of the .NET Framework for Windows Server 2008 Server Core Edition R2. You can read about it at http://technet.microsoft.com/ library/dd883268.aspx. However, this version of the .NET Framework still has significant limitations and you might actually find it better to use Mono for your .NET applications. For example, while you can now provide limited support for ASP.NET on Server Core, you might actually find the Mono alternative, XSP, to provide the solutions you need for your application.
Mono has generated quite a bit of interest from the Server Core crowd, especially anyone who uses Server Core as their main server. Server Core has a number of advantages that makes it popular with small- to medium-sized companies. It uses far less memory and other resources, runs faster, runs more reliably, and has a far smaller attack surface for those nefarious individuals who want to ruin your day by attacking your server. You can find a complete article about running applications on Server Core using Mono at http://www.devsource.com/c/a/Architecture/ Mixing-Server-Core-with-NET-Applications/.
Obtaining and Installing Mono
It’s time to obtain and install your copy of Mono. Of course, the first step is to download the product. You can find the various versions of Mono at http://www.go-mono.com/mono-downloads/ download.html. This section assumes you’re installing Mono version 2.6.1 on a Windows system. If you need to install Mono on another system, follow the instructions that the Mono Web site provides for those versions. After you complete the download, follow these steps to perform the installation.
- Double-click the mono-2.6.1-gtksharp-2.12.9-win32-1.exe file you downloaded from the Mono Web site. You see a Welcome page.
- Click Next. You see a License page.
- Read the licensing information. Select I Accept the Agreement, and then click Next. You see the Information page shown in Figure 19-1. Unlike most Information pages, this one actually contains a lot of useful information. Make sure you review the information it contains and click on the links it provides as needed. Especially important for keeping updated on Mono is joining the mailing list (http://www.mono-project.com/Mailing_Lists) or forums (http://www.go-mono.org/forums/). You can find these links at the bottom of the Information page.
- Read the release information and then click Next. You see the Select Destination Location page shown in Figure 19-2. Normally, you can accept the default installation location. Some developers prefer a less complex path to Mono, such as simply C:Mono, to make it easier to access from the command line. The chapter uses the default installation location.
- Provide an installation location for Mono and then click Next. You see the Select Components page shown in Figure 19-3. The components you select depend on what you plan to do with Mono — you can always change your setup later if necessary. If your only goal is to try Mono for your existing .NET applications and to create some simple IronPython applications, you really don’t need the Gtk# and XSP support. This chapter assumes that you perform a Compact Installation to obtain a minimum amount of support for working with the IronPython sample application.
- Select the components you want to install and then click Next. You see the Select Start Menu Folder page. This is where you choose a name for the folder that holds the Mono components. The default name normally works fine.
- Type a name for the Start menu folder (or simply accept the default) and then click Next. You see the Ready to Install page. This page provides a summary of the options that you’ve selected.
- Review the installation options and then click Install. You see the Installing page while the installer installs Mono on your machine. After a few minutes, you see a completion dialog box.
- Click Finish. You’re ready to begin using Mono.
Creating an IronPython Application with Mono
It’s time to begin working with Mono and IronPython to create an application. Of course, you’ll want to know a bit more about how Mono works before you just plunge into the project, so the first step is to look at Mono from a command line perspective. The first section that follows shows how to create an IPY environment variable and use it to open the IronPython console using Mono whenever you need it. The sections that follow show how to create a project, build a simple IronPython application, and then test the application in a number of ways.
Working at the Command Line
Mono works differently than the .NET Framework. When you want to use the .NET Framework to execute an application, you simply double-click the application and it starts. The same doesn’t hold true for Mono. If you want to execute an application using Mono, you must open the Mono command prompt and start it by specifically specifying Mono. Unfortunately, this limitation has an unusual effect on working with IronPython because you can no longer access IPY.EXE using the Path environment variable. Instead, you must create a special IPY environment variable using the following steps.
- Double-click the System applet in the Control Panel and choose the Advanced tab. You see the System Properties dialog box.
- Click Environment Variables. You see the Environment Variables dialog box.
- Click New in the System Variables section of the Environment Variables dialog box if you want to use IronPython from any account on the machine or the User Variables section if you want to use IronPython only from your personal account. You see a New System Variable or New User Variable dialog box. Except for the title, both dialog boxes are the same.
- Type IPY in the Variable Name field.
- Type C:Program FilesIronPython 2.6 or the location of your IronPython installation in the Variable Value field.
- Click OK three times to add the new environment variable, close the Environment Variables dialog box, and close the System Properties dialog box. You’re ready to begin working with IronPython.
At this point, you’re ready to begin working with Mono. Choose Start ➪ Programs ➪ Mono 2.6.1 for Windows ➪ Mono-2.6.1 Command Prompt to display a Mono command prompt. When you see the Mono command prompt, type Mono “%IPY%IPY.EXE” and press Enter. You’ll see the usual IronPython console.
The first thing you should notice is that the .NET Framework version reporting by the IronPython console is slightly different from the one you normally see. There isn’t any problem with this difference. In fact, it’s the only difference you’re going to notice as you work with the IronPython console. Let’s give it a try so you can see for yourself. Type the following code and you’ll see the standard responses shown in Figure 19-4.
for ThisPath in sys.path:
If you compare the results you see when running IronPython under the .NET Framework with the results you see when running IronPython under Mono, you won’t notice any differences. In fact, you can try out the applications in this book, and you won’t see any differences at all unless you need to work with an extension or other outside code source (and you might not even see any differences then). Working with Mono simply means you have access to more platforms when working with IronPython, not that you have more limitations.
Defining the Project
The project you create for working with Mono is going to be just a little different from the one you create when working strictly with the .NET Framework. You’ll still start up IronPython using the Visual Studio IDE, but there’s an extra step now: you must start Mono first.
- Choose File ➪ Open ➪ Project/Solution. You see the Open Project dialog box shown in Figure 19-5.
- Highlight Mono.EXE in the Program FilesMono-2.6.1bin folder of your machine (unless you used a different installation folder) and click Open. Visual Studio creates a solution based on Mono.
- Right-click the Mono entry in Solution Explorer and choose Properties from the context menu. You see the General tab of the Properties window shown in Figure 19-6.
- Type “C:Program FilesIronPython 2.6IPY.EXE“ -D TestMono.py in the Arguments field (change the folder location to match your IronPython installation).
- Click the ellipses in the Working Directory field to display the Browse for Folder dialog box. Locate the folder that contains the project you’re working on and click OK. The project folder appears in the Working Directory field of the Properties window.
- Choose File ➪ Save All. You see a Save File As dialog box.
- Type the solution name in the Object Name dialog box and click Save.
- Right-click the solution entry in Solution Explorer and choose Add ➪ New Item. You see the Add New Item dialog box.
- Highlight the Text File template. Type TestMono.py in the Name field and click Add. Visual Studio adds the Python file to your project and automatically opens it for you.
Creating the Code
It’s time to add some code to the IronPython file. This example provides a listing of the modules that IronPython is using. If you compare this list to the one that IronPython provides when you run the application using the .NET Framework, you’ll see the modules in a different order, but otherwise the output is the same. Listing 19-1 shows the code used for this example.
Listin g 19-1: Creating a simple Mono test program
# Obtain access to the sys module.
# Output a list of modules.
print ‘IronPython Module Listingn’
for ThisMod in sys.modules:
print ThisMod, sys.modules[ThisMod]
# Pause after the debug session.
raw_input(‘nPress any key to continue…’)
This example demonstrates a simple for loop to iterate through the list of modules found in the sys .modules dictionary. In this case, the code prints out two items. First, it prints out the module name. Second, it prints out the module information, which normally includes the module location. As always, the code ends with a pause, raw_input(), so that you can see the output before the window closes.
Running the Application from the IDE
Running the application is the first place you see some potential problems with using Mono. If you click Start Debugging, you see the No Debugging Information dialog box shown in Figure 19-7. If you click Yes, the program will run, but you won’t get any debugging support. This is one of the problems with using Mono exclusively. You’ll probably want to use the normal .NET Framework setup to debug your application first, and then move on to the Mono configuration described in this chapter to test the application under Mono.
To start the application successfully, choose Debug ➪ Start Without Debugging or press Ctrl+F5. The program will run normally and you’ll see the usual message at the end. Pressing Enter displays a second pause as shown in Figure 19-8. It seems that Mono provides its own pause so that you can see the results of executing the program, which is a nice touch for those times when you forget to add a pause of your own.
Running the Application from the Command Line
Interestingly enough, Mono does come with direct support for IronPython, but Mono supports IronPython 1.1, and the IronPython console supplied with Mono seems to do odd things. Open a Mono command prompt, type IPY, and press Enter. Now try typing 1+1 and pressing Enter. You’ll probably see results like those in Figure 19-9.
Of course, the question isn’t about the IronPython console, but whether it can run the example application. Press Ctrl+C to break out of the mess you’re seeing onscreen. Type Y and press Enter when you’re asked whether you want to stop the batch file. Then type IPY TestMono.py and press Enter. You’ll see that the application does indeed work, as shown in Figure 19-10. The number of modules is far smaller than the list shown in Figure 19-8, but it’s correct for the version of IronPython provided with Mono.
The picture isn’t completely gloomy. Developers are constantly trying new solutions for working with IronPython. You can find a potential fix for the problems described in this section of the chapter at http://ironpython-urls.blogspot.com/2009/06/mono-can-now-compile-ironpython-20 .html. The solution comes with the caveat that it might not work for you.
Interacting with Other .NET Languages under Mono
Mono originally focused its attention on C# development, but later added support for Visual Basic .NET as well. At this point, you can run any Visual Studio 2008–created application written under C# or Visual Basic.NET using Mono within the limitations described in the section “Understanding Mono Limitations” earlier in this chapter. Even DLR code appears to run fine in Mono within the current limits of the product, which aren’t many.