Thursday, August 13, 2009

Installing support for .NET development

This is a follow-up from Installing support for ColdFusion development.

Fair warning: This is not a definitive tutorial, it's just an informal record of my experiences in trying to install software for .NET development on Ubuntu. I had not done any .NET development prior to this. Maybe in a future blog post I'll have something more interesting to share.

.NET development is supported on Linux by the Open Source project mono, at http://www.mono-project.com. In reading about this on the web, I learned that the Ubuntu distro includes support for running .NET applications with mono, but not for developing them. My first step, then, was to install the basic mono package:
sudo apt-get install mono-devel
To verify the installation worked, I followed the instructions at http://www.mono-project.com/Mono_Basics to compile and run a few simple Hello World programs.

The first is a command-line Hello World that looks like this:
using System;

public class HelloWorld
{
static public void Main ()
{
Console.WriteLine ("Hello Mono World");
}

}
To produce a .NET 2.0 assembly, I compiled with
gmcs hello1.cs
No errors reported. To execute the program, I used
mono hello1.exe
It worked fine.

BTW this was my first-ever C# program. I'm so proud! :-) Oh, wait. I didn't test-drive it. I'm so ashamed. :-(

The second test was a gtk# version of the same thing. It looks like this:
using Gtk;
using System;

class Hello {

static void Main()
{
Application.Init ();

Window window = new Window ("helloworld");
window.Show();

Application.Run ();

}
}
and is compiled like this:
gmcs hello2.cs -pkg:gtk-sharp-2.0
I ran it with
mono hello2.exe
and it worked fine.

Next there was a Winforms sample. It looks like this:
using System;
using System.Windows.Forms;

public class HelloWorld : Form
{
static public void Main ()
{
Application.Run (new HelloWorld ());
}

public HelloWorld ()
{
Text = "Hello Mono World";
}
}
I compiled it according to the instructions with this command:
gmcs hello3.cs -pkg:dotnet
This one didn't go quite so well. It complained about 13 missing dlls.

I found a suggestion at http://code.google.com/p/paint-mono/issues/detail?id=6 to try this:
sudo apt-get install libmono-winforms2.0-cil
and compile with
gmcs hello3.cs -r:System.Windows.Forms.dll
This time the compile worked. I tried running the program with
mono hello3.exe
and it worked fine.

So at that point I had at least a partial installation of basic mono development tools. Clearly, there were still missing pieces, but at the time I did not know what more to look for.

MonoDevelop IDE


This is an IDE for working with mono. The home page is http://www.monodevelop.com. I installed it with
sudo apt-get install monodevelop
Mono Develop appeared under Applications -> Programming and it started up nicely. I didn't know what more to check, so I declared victory and moved on.

NUnit (unit testing tool)


I didn't find any useful help on the NUnit site at http://www.nunit.org, so I searched the web and found some help at stackoverflow.com/questions/924239/what-the-best-setup-latest-mono-monodevelop-on-ubuntu-9-04. They suggested this:
sudo apt-get install
monodevelop-nunit monodevelop-debugger-mdb
As far as I could tell, this appeared to work.

NUnit with MonoDevelop


I had to search for help with this, as well. There doesn't seem to be any single comprehensive source of "getting started" information for people who want to learn to work with mono. At http://www.dijksterhuis.org/using-nunit-with-monodevelop/ Martijn Dijksterhuis suggests the following.

First, create a new Solution in MonoDevelop. I chose Console Project and skipped the options presented in the Project Features dialog. Then, right-click the References item in the Solution tree menu in the left-hand pane. Choose Edit References and select NUnit.Core and NUnit.Framework to include them in the Solution. What I found at that point were two options named nunit.core (all lower case) and two options named nunit.framework. One of each had a version number. I chose the entries that had version numbers.

Next step was to paste in the sample program Martijn had posted:
using System;
using NUnit.Framework;

namespace bank
{
public class Account
{
private float balance;
public void Deposit(float amount)
{
balance += amount;
}

public void Withdraw(float amount)
{
balance -= amount;
}

public void TransferFunds(Account destination, float amount)
{
}

public float Balance
{
get { return balance; }
}
}
}

namespace bank
{
[TestFixture]
public class AccountTest
{
[Test]
public void TransferFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Account destination = new Account();
destination.Deposit(150.00F);

source.TransferFunds(destination, 100.00F);
Assert.AreEqual(250.00F, destination.Balance);
Assert.AreEqual(100.00F, source.Balance);

}

[Test]
public void DepositFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Assert.AreEqual(200.00F, source.Balance);
}

}
}

namespace UnitTestDemo
{
public class MyAccountingSoftware
{

public static void Main()
{
bank.Account DemoAccount = new bank.Account();
DemoAccount.Deposit(1000.00F);
DemoAccount.Withdraw(500.50F);
Console.WriteLine("Our account balance is {0}", DemoAccount.Balance);
}

}
}
I chose View -> Unit Tests from the MonoDevelop menubar and right-clicked my Solution, then chose View -> Test Results to see the results. It appeared as if NUnit ran, and the sample program had 1 passing and 1 failing test. Looked good enough as a start.

Next: Pimp My Instiki

No comments:

Post a Comment