My Life As A Blog

Archive for the ‘c#’ Category

Simple Configuration with IronPython and .NET 4

with 2 comments

Recently I posted a short article about how to do simple configuration with IronPython.  I figured that it would be easier with .NET 4.0 thanks to the dynamic support.  And it is.  Using Visual Studio 2010, create a new Console Application.  Add one file called and set the copy output property to Copy Always.  Here’s the Python code for that file:

configuration.Text = "Hello from IronPython"
configuration.Count = 4

And here’s the code for the Program class:

class Program
    static void Main(string[] args)
        dynamic configuration = ConfigureFromIronPython();

        for (int i = 0; i < configuration.Count; i++)

        Console.WriteLine("Press any key to exit...");

    private static dynamic ConfigureFromIronPython()
        dynamic configuration = new ExpandoObject();
        ScriptEngine engine = Python.CreateEngine();
        ScriptScope scope = engine.CreateScope();
        scope.SetVariable("configuration", configuration);
        engine.ExecuteFile("", scope);
        return configuration;

For that to work you need add references to IronPython and Microsoft.Scripting (there’s a CTP of IronPython for .NET 4.0 that you can get here.)  You’ll also need a few using statements:

using System.Dynamic;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

The two things that make this work are the ExpandoObject and the dynamic keyword.  The ExpandoObject is a dynamic property bag that allows us to set and get members at runtime as needed.  The dynamic keyword means that .NET will resolve the properties at runtime (rather than the traditional behaviour of checking at compile time.)  The result is (I think) more simple and elegant than the configuration code to which I’ve become accustomed.


Written by remark

September 28, 2009 at 12:53 pm

Posted in .NET, c#, Design, Python

Simple configuration using IronPython

leave a comment »

I’ve just posted an article on how to use IronPython to configure an application – read it here.

Written by remark

September 14, 2009 at 4:49 pm

Posted in .NET, c#, Design, Development, Python

Use .NET classes in IronPython

with one comment

It’s really simple to use a .NET class in IronPython.  The first thing to remember is that you’ll need to add a reference (just like you would in .NET).  To do that you use the AddReference method of the clr module.  In the case of an assembly you’ve written, IronPython needs to be able to find it, which means it needs to be in sys.path.  It turns out that you can add the path to your assembly by using the append method of sys.path.  Here’s a simple example.  First, let’s create a simple class called User in C# in a solution called SampleClasses:

namespace SampleClasses


    public class User


        public string Name{ get; set;}

        public DateTime DateOfBirth { get; set; }



For the sake of simplicity, let’s copy the dll to a folder called lib on the C drive.  OK.  Time to fire up IronPython (which I’m going to assume you’ve already installed.)  Open a command prompt and type “ipy”.  You should see something like the following:


Next, let’s ensure the SampleClasses assembly is available to IronPython:

>>>import sys

Once we’ve done that we can add a reference:

>>>import clr

Now, we need to import the User name from the SampleClasses namespace:

>>>from SampleClasses import User

We’re all set.  Create an instance of user and set one of the properties:


That’s all there is to it.  Now you can go and experiment with IronPython and classes you’ve already written in .NET.

Written by remark

June 4, 2009 at 2:49 pm

Posted in .NET, c#, Python, Software

AIC 2009 – Dynamic Languages and Architecture

leave a comment »

Thanks to all of you who attended my session at AIC earlier today.  The slides will be made available on line over the next week or so. 

I think the interesting capability made possible by the DLR is using static languages and dynamic languages together.  And there’s another benefit of learning a new language: when we only use one language we tend to think in that language – having other languages in our toolkit means that we have other approaches available to us.

So, where can you start to take advantage of dynamic languages?  The areas I discussed today were:

  • extending your application by adding scripting support to your application
  • configuring your application with a dynamic language
  • creating a DSL using a dynamic language
  • writing one or more layers of your architecture in a dynamic language
  • testing your application(s) with a dynamic language

Of all of these, extending and testing are probably the best places to start.

I also talked a bit about the DLR (and a couple of the Iron Languages – Iron Python and Iron Ruby) and the way that C# will be taking advantage of the DLR. It’s fascinating to see the evolution of programming languages and how the trends of dynamic, functional and concurrent programming are influencing this evolution.

Here are the links that I gave out in my session:

Dynamic Languages on .NET (

I also mentioned the Anders Hejlsberg session on the future of C# – you can watch that here – and a Channel 9 video of Anders Hejlsberg and Gilad Bracha discussing language design, which you can find here.

As part of the preparation for the session, I exchanged some emails with a few folks including Michael Foord.  For those of you who’d like to see Michael’s take on this subject, he’s posted about it here.

Written by remark

May 8, 2009 at 5:29 pm

Adding mobility to an ASP. NET MVC site

leave a comment »

I’ve posted a short article about how I added mobile support to the iWantGreatCare Proof of Concept.

Written by remark

April 16, 2009 at 5:35 pm

To Your Good Health

leave a comment »

I’ve been busy with a couple of Proofs of Concept recently. The second of these that I worked on was iWantGreatCare. iWantGreatCare is a website that lets you submit reviews of doctors, both GPs and specialists. The existing site is built using Ruby on Rails and MySql. The Proof of Concept (PoC) was built in three weeks by a small team from Microsoft, iWantGreatCare and DotNetSolutions.

We had a few objectives for the PoC. We wanted to show:

  • Integration with healthcare providers’ systems
  • Intuitive, engaging user experience
  • Extensible architecture
  • Fit of Microsoft Technologies

We focused on a key scenario based around a mother with a son who requires medical attention who has just moved to a new area. This scenario covers finding a GP, reviewing the GP, finding a specialist and reviewing that specialist. We also looked at how healthcare providers could use and analyse the data that is created by these reviews.


We chose to build the site in ASP .NET MVC (version 1.0 of which has just been released.) Using ASP .NET MVC allowed us to build the site quickly and we were also able to extend it easily to provide different views (for instance, mobile phones and RSS feeds.) It is also readily learned by Ruby on Rails developers. The geospatial features of SQL Server 2008 made the location based searching far simpler than the existing implementation (we used Virtual Earth to show locations) – and Reporting Services provided drill-down reports that can be consumed by healthcare providers through the site. We also demonstrated integration with Sharepoint – a common technology in the NHS.

At the conclusion of the PoC, we had a working application that included all the features we had planned along with a couple of extras (the RSS feeds being an example.) ASP .NET MVC and the speed at which we were able to develop made this possible. iWantGreatCare are already well advanced with the migration to SQL Server 2008, and I look forward to seeing the ways in which the site develops.

Written by remark

March 20, 2009 at 5:32 pm

Posted in .NET, Architecture, c#, Microsoft

Tagged with

An Intelligent City

with 2 comments

Last week I presented the results of a Proof of Concept (PoC) at the Local Authority Urban Interest Conference, a sub group of ITS United Kingdom, which is the intelligent transport society for the UK. The PoC was completed the week before in the Microsoft Technology Centre (MTC). We had a team that included people from Microsoft, Birmingham City Council, Coventry University Enterprises, Shoothill and EMC Consulting. A PoC in the MTC is a three week exercise so you need clear objectives and a focussed team.

The Intelligent City programme is a Birmingham City wide initiative to “address key urban issues relating to transport, tourism, security and climate change through the exploitation of information technology”.

As with any PoC, there are some proof points. In this case the key points are to:

  • Articulate and demonstrate a slice of the shared Intelligent Cities vision for Birmingham
  • Demonstrate an interoperable service layer platform integrated with existing data / services
  • Demonstrate compelling user experiences cross-device – manage a journey tracking and hopping across devices and modes of transport
  • Demonstrate empowering individuals to make more informed, smarter choices

For the PoC, we focussed on transport and looked at a couple of specific scenarios. The first scenario is that of someone – we called him Colin – staying in a hotel in Birmingham who wants to visit his aunt during his time there. The second scenario focuses on Colin driving into the centre of Birmingham to go to the offices of a client.

We had data about the road network, the bus routes and schedule as well as access to a service that gave us real time positions of buses on some routes and data of traffic levels.

At the heart of the solution is a service layer that is able to create, store and share routes using that data and services we had available to us. We also built 3 client applications that used this service layer: a website, a mobile application and a mock sat-nav application. As you’d expect, the service layer is WCF, C# and a SQL Server 2008 database.

It’s probably worth saying a little about the routing before going any further. The routing algorithm had a model we built up of the road and bus network (including the schedule we had) so it could provide multi-modal routes across Birmingham – there is no park and ride scheme as such in Birmingham, so a car route is a car-only route for the purposes of the PoC. The way the model was created means that adding in other forms of transport would be relatively simple, provided you know where the modes of transport intersect (e.g. where a train station or car park is.) Adding in realtime information about journey times would also be simple.

The website is an ASP.NET App that shows traffic levels in Birmingham along with the realtime bus positions we have layered on Virtual Earth. It can also be used to get a route across Birmingham – once a route has been created, the user can log in and save the journey. The journey can also be shared – this allows other users with whom the journey has been shared to see your progress along the route.


The mobile application is a Silverlight app that addresses a problem we all face getting around. It’s all very well plotting a route on a website, but can you remember it? You can print it out, but the print out doesn’t know where you are. A mobile application can take advantage of GPS, which is increasingly prevalent in mobile devices. You can log in and retrieve the route you previously saved and the application will guide you along your route – including getting on and off buses. If you’ve chosen to share your route, it’ll upload this information so those people with whom you’ve shared your route can see where you are and when you’re expected to arrive.

Mobile UI

The mock sat-nav application is a WPF application that is intended to show what a sat-nav could do to take advantage of the service layer. It shows how you could choose to be directed to a public car park near to your destination (you could use the mobile app to get from the car park to your destination.) During the journey, the car park fills up, so the sat-nav alerts you and directs you to the next available car park.


Having the service layer made each of these applications quick to develop – and allows the development of other applications. Demonstrating these applications brings that point home and shows how a big difference can be made to our experience of Birmingham in the very near future.

Written by remark

March 12, 2009 at 7:35 pm