Saturday, December 8, 2007

Running C# .NET Applications On Fedora 8 Linux

One of the main challenges and the reason most corporations choose to target a windows operating system for their custom applications is that developing custom linux applications is not something that most entry level developers know how to do. To acomplish this they would have to rely on some of the most unreliable groups of programmers to control in the business: The Linux GUI or Java GUI Developers, both of which are hard to find, and even harder to manage. Some have even been known to walk off of jobs just because they weren't given enough of a say on how their master pieces should run and function!

The .NET Framework is changing all of that through the use of the Mono Project. The Mono Framework, is a Framework that is essentially a carbon copy of all the libraries in the original .NET Framework, compiled for other platforms like Linux, Mac OS X, Solaris and even Nokia 770 and 800 Devices! This puts the .NET Framework a step ahead of other frameworks like Java, because of the fact that it turns your standard C# developer (very easy to please group) to do the work that Java type developers used to do. Needless to say that this phenomenon is speading like wildfire through more and more coprorations, because now you can develop applications on your standard Windows .NET Development environment, and have it run on Linux, which comes free! To make things even more convinient, you can even write Webservices and other types of .NET Applications for Linux!

It shouldn't come as a big surprise therefore that Fedora 8 Linux distribution (Redhat) comes with the Mono framework pre-installed fresh out of the box. I have also noticed a surge in the number of open source C# linux projects on open source community sites like SourceForge.net that take advantage of the Mono framework to write fairly complex linux applications in record time, and take advantage of the CLR type debugging that the framework has to offer to find bugs and fix them even faster.

Most developers at one point or another have thought about installing a linux OS on a Virtual Machine like "Microsoft Virtual PC 2007", which can be downloaded for free HERE simply because that way they wouldn't have to take up a whole PC for testing on a Linux OS, and at the same time this allows them to try out several different flavors (distributions) of linux on the same machine. Most of you however are discouraged when you notice that neither your Mouse, Nor your Display (either one or both) functions, which eventually forces you to delete the whole virtual PC and forget about linux yet once more, cursing Microsoft for not making their Programs a bit more competitor friendly! I am here however to tell you that it IS POSSIBLE to run a Linux OS on a Virtual Machine (with a few tweaks to the grub booting agent).

THE OPERATING SYSTEM - Fedora 8

For my experiment with the Mono Framework, I choose Fedora 8, since I have worked with other Redhat products in the past, and find it to be one of the most active Linux distributions out there. I probably would have considered Open SuSE 10.3 instead, if they had the mono framework preinstalled on it like Fedora 8 does (call me lazy, but that's a good hour or two that I don't have to keep messing with the OS to get mono working correctly, which I can spend on other thing like figuring out how to run the OS on Virtual PC and work).

So go ahead and grab the ISO image for Fedora 8 from HERE. This is the DVD ISO image, which you have to burn on a DVD to install Fedora 8. Other download options can be found HERE. Be prepared to take a nice long 3-4 hour coffee break while it is downloading, just don't overdue it with the coffee, because then you got to take a bathroom break every 5 minutes while trying to do the work, and that makes it hard to focus on the task at hand. :)

While you are waiting for the Fedora image to download, go ahead and grab a copy of Microsoft Virtual PC 2007 for Free (special thanks to our good friend Billy Gates for that) from HERE. I assume that everyone knows how to do a simple install of this, so I am not going to walk you through that part. I ran my experiment on a Windows XP SP 2, but Virtual PC does also install on Windows Vista (even though you get a warning that it is not a supported OS, but it still runs). Run Virtual PC, and you should see this screen:



Click on "New" to create a new virtual PC. Hit "Next" to go to the next screen. Choose "Create a Virtual Machine" and hit "Next". On the next screen, enter "Fedora 8" for description and go to the next screen. Notice that it automatically selects OS of "Other" for you. Accept it and hit "Next". Check "Adjust the Ram" and enter 512 for your RAM. Select "A New Virtual Hard Disk", go to the next screen and select the default path and hard drive size and click "Finish" to complete.

Hopefully your Fedora 8 download is done at this point. Burn the ISO image to a DVD with your favorite DVD burning software and pop it in your drive. You should have a new icon in your Virtual PC Console called "Fedora 8" at this point. Select it and click on "Start" to start the install process. You may have to reset the virtual machine once to get thei Fedora 8 installer to work right. Go to "Action > Reset" to reboot the Virtual Machine.

The actual Fedora 8 installation is sadly not a very straight forward process, because you will have to tweak it to get the Mouse and Display to work right. I am not going to go through every detail of the install process, but to get the Display and Mouse to work correctly:
  • On Install: Hit the button and add the following to the boot line:

    vmlinuz initrd=initrd.img vesa i8042.noloop
    After Install: Stop reboot at the grup by hitting the ESC key or Spacebar. Press "e" to edit the boot line, and add the "i8042.noloop" to the second line. Hit "Enter" key followed by pressing the "b" key to boot.

  • To Fix Mouse Problem for good: After logging in, go to Applications > System Tools > Terminal, and enter the following:


    su
    nano /boot/grub/menu.lst
    Go down to the line that starts with "Kernel" and add "i8042.noloop" to the end of that line. Press Ctrl+O to write out, hit "Enter" followed by a Ctrl+X to exit out. Reboot and you should be good to go.
Running Standard .NET Applications

Lets first start by logging in as your regular user. I have created two test applications using C#, which can be downloaded from my website. The Command Line Test Application can be downloaded HERE and the GUI TEST Application from HERE. Both of those are simple "Hello World!" type applications written in C#. One outputs it to the Command Prompt and the other pops up a GUI messagebox for it, to proove the concept.

If you are a hardcore windows application developer, the first thing you will do is to double click on the executables once you have them downloaded on your Fedora Desktop. If you did that, you would see the following message:



You are NOT in Windows any more, so snap out of it! You can't just go around double clicking on a windows application on Linux! What you have to do instead is to call mono and have it launch your program for you instead. So lets do that real quick. Go to "Applications > System Tools > Terminal" and open a new Terminal window. Next, navigate to the folder you downloaded the "exe" files to (in my case its "/home/Pedram/Documents") and type "mono HelloWorldCS.exe". You will get the output "Hello World!" back, showing you that everything is working OK. Congratulations! You have just run your first .NET Application on Linux! Here is how your screen should look like:



Getting our GUI Application to run is a bit more complicated then this. Don't get me wrong, the process is exactly the same. But there are some file permission issues with it that I myself have yet to figure out. So lets do the same for our GUI Application. Type "mono HelloWorldGUICS.exe" in your Terminal Window. Here is what you expect to see:



But instead, the first time you run it, you will see the following error:



That's because the first time you run the GUI "exe", mono tries to recompile it to a Linux compatible application before runnning it, but does not have permission to several critical GUI related libraries! Log out as your regular user, log back in as "root" user and try the same thing. This time you should get the correct message pop up.

If you now go back and log in as your original user, you will notice that you can now run this application as your regular user as well! That's because the GUI Application has already been compiled by the root user, and Mono grabs that compiled version when you try to run the Application under a regular user.

At this point you are probably ready to shout for joy, jump up and down and start coding all kinds of weird .NET GUI Applications for Linux. But with Fedora 8 being a newly release Operating System, and having the advanced security system on it that kind of behaves like Vista Security, there will be all kinds of error messages that will pop up here and there that will puzzle the heck out of you. If you do come across some weird error message that you would like to share with me or get my opinion on, email me at webmaster@digioz.com to talk about it. Best of luck with Mono!

Update 09/10/2010 - It has been quite a while since I wrote this blog entry. Fedora has had quite a few releases since this blog was written (they are now up to Fedora 13, where as in this blog I tested this on Fedora 8). Today I tested this same .NET Application, both command line and GUI on Fedora 13. Everything worked as before, with the only exception that for some reason Mono is no longer included with Fedora. You can however download it from this link and install the RPM to do your testing:


http://rpm.pbone.net/index.php3/stat/4/idpl/14046412/dir/fedora_13/com/mono-core-2.6.1-2.fc13.i686.rpm.html

Pete Soheil
DigiOz Multimedia
http://www.digioz.com/

1 comment:

Unknown said...

Great
http://programing-csharp.blogspot.com