Sunday, April 27, 2008

Getting a list of services loaded into the workflow runtime

Getting a list of the services loaded into the workflow runtime is pretty simple. Here's some code to do it.

using System;
using System.Collections.ObjectModel;
using System.Workflow.Runtime;

namespace GetAllServices
{
  class Program
  {
    static void Main(string[] args)
    {
      WorkflowRuntime runtime = new WorkflowRuntime();
      runtime.StartRuntime();
      ReadOnlyCollection<object> services = runtime.GetAllServices(typeof(object));
      for (int i = 0; i < services.Count; i++)
      {
        Console.WriteLine(services[i].ToString());
      }
      Console.ReadLine();
    }
  }
}

If you try to get a service from inside an activity using the ActivityExecutionContext.GetService method, the available services will be different. Looking in Reflector, you'll see there are several services that won't be returned and the IStartWorkflow service will be created on the fly. Also have a look at WorkflowExecutor.GetService called from ActivityExecutionContext.GetService which also special cases some services before finally calling the runtime's GetService.

Saturday, April 26, 2008

Invoking a XOML workflow from a workflow

The built-in InvokeWorkflow activity requires a compiled workflow type which isn't too useful if you're trying to execute a XOML-based workflow. Initially I thought I could compile the XOML workflow and then use the InvokeWorkflow activity. The problem with this approach is that the InvokeWorkflow's TargetWorkflow property can't be set at run-time.

The solution is pretty straightforward, compile the workflow then use the IStartWorkflow service to start the workflow (which is what InvokeWorkflow does internally). But there are still a couple of issues with this.

First, there is no way to pass parameters to a XOML workflow, since there is no way to add top-level properties. You could workaround this by sub-classing the SequentialWorkflowActivity and adding properties for the parameters you want to pass.

The second problem is if you want the child workflow to finish before continuing with the parent workflow. rather than starting the child workflow asynchronously. I've seen examples of how to do this by the child workflow signalling when it's complete but I've not seen this done in a generic way that doesn't require the child workflow to be modified.

Anyway, here's my code which might be useful.

using System;
using System.IO;
using System.Reflection;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using System.Xml;

namespace WFBuild.Activities
{
  public class InvokeXomlWorkflowActivity: Activity
  {
    public InvokeXomlWorkflowActivity()
    {
    }

    private string xomlFile;
    public string XomlFile
    {
      get { return xomlFile; }
      set { xomlFile = value; }
    }

    protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
    {
      string className = Path.GetFileNameWithoutExtension(xomlFile);

      // compile workflow
      WorkflowCompiler compiler = new WorkflowCompiler();
      WorkflowCompilerParameters wfParams = new WorkflowCompilerParameters();
      wfParams.LibraryPaths.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
      wfParams.ReferencedAssemblies.Add("WFBuild.Activities.dll");
      wfParams.OutputAssembly = string.Format("{0}.dll", className);

      // add x:Class attribute
      string tempFileName = Path.GetTempPath() + "temp.xoml";
      XmlDocument doc = new XmlDocument();
      doc.Load(xomlFile);
      const string namespaceUri = "http://schemas.microsoft.com/winfx/2006/xaml";
      if (doc.DocumentElement.GetAttribute("Class", namespaceUri) == "")
      {
        doc.DocumentElement.SetAttribute("Class", namespaceUri, className);
      }
      doc.Save(tempFileName);

      WorkflowCompilerResults results = compiler.Compile(wfParams, tempFileName);

      if (results.Errors.HasErrors)
      {
        string errors = "";
        for (int i = 0; i < results.Errors.Count; i++)
        {
          errors += results.Errors[i].ErrorText + "\n";
        }
        throw new Exception(errors);
      }
      
      Type[] types = results.CompiledAssembly.GetTypes();
      Type workflow = types[0];

      IStartWorkflow service = executionContext.GetService(typeof(IStartWorkflow)) as IStartWorkflow;
      if (service == null)
      {
        throw new InvalidOperationException("No IStartWorkflow service available");
      }
      Guid guid = service.StartWorkflow(workflow, null);
      if (guid == Guid.Empty)
      {
        throw new InvalidOperationException("Failed to start the workflow");
      }
      return ActivityExecutionStatus.Closed;
    }
  }
}

Tuesday, April 22, 2008

Become a money lender

OK, perhaps my dream of a world without money lenders is a pipe dream, but if that isn't a possibility then how about we cut out the middle man and do the lending ourselves? OK, we can't completely cut out the middle man (unless we lend out money to random people down the pub), but we can use a middle man to lend or borrow money directly, rather than effectively lending our money to the bank for a pitiful return.

Enter Zopa, who if nothing else, prove that all the good domain names have been taken. Basically you put your money in their system and offer to lend it out at whatever rate you like and it is lent out to people directly for a small fee. They work out the credit-worthiness of borrowers but you take the hit when borrowers default. The claimed interest rates are better than you'll get in a bank account, although it is still taxable. Saying that, tax isn't taken at source, so it's certainly possible to avoid paying tax on the interest earned (although that would clearly be a very bad thing to do...). It's called social lending and I like the idea.

The only downside is that your money is tied up for a potentially long time, you can't get the money back until the loan is paid off. I've put in a few hundred quid to see how well it works out. If it's a success I'll probably start to put money in on a regular basis. My current return? 11p from interest earned on the money sitting in the account waiting to be loaned out.

Monday, April 21, 2008

In defence of geeks

There's an ad for BT on the telly currently in which a mother talks to her partner about her worries for her daughter who's spending lots of time on the internet. She pauses for dramatic effect, as we are left to think of the terrible things that could be happening to the kid. So what is she concerned about? Some sexual predator grooming her child? Oh no, it's much much worse than that... She's worried that her daughter is turning into a geek.

Yeah OK, it's all light-hearted enough, but it misses an important point. If it weren't for geeks, there'd be no internet, no eBay, Amazon, Facebook and all the other crap that we can pass the time with. There'd be no computers for that matter, or iPods or mobile phones or CDs or any other electrical gadgets. I'm sure it was geeks (before they were called that) who developed cars, electrical power generation and the telephone.

So let's all the hail the geek, for producing all this stuff to make our lives so much richer (there is a hint of sarcasm there...)

Sunday, April 20, 2008

More on 0871 numbers

Somebody posted a comment to my little rant about 0871 numbers. Initially I thought it was just someone trying to defend the use of 0871 numbers but then I noticed the link to some site that sells 0871 numbers. A little search of the internet showed the same guy had posted comments to several other blogs with the same link. So a little spam on my blog, no problem, just delete the comment. But then I found a site that is trying to help people to avoid having to use 0871 numbers and other variants, so I thought I'd post a link to that site to help them out and to show spammers that their tactics may well have completely the opposite effect to what they were after...

Friday, April 11, 2008

Live Maps, what the ...?

As of today, if you go to Live Maps and you're based in the UK, you get redirected to Multimap instead. It's still possible to get to the real Live Maps via the URL http://maps.live.com/?mkt=en-us, but why on earth has Microsoft decided to do this? Live Maps has improved drastically over the last few months and is now at the very least on a par with Google Maps. Multimap, whilst certainly better than it was, is still way behind both of them.

All I can think is that Microsoft spent a significant amount of money on Multimap so isn't prepared to let it die a natural death. Multimap is plastered with ads as well so I guess they will earn some cash from it, something that presumably isn't happening with Live Maps. Unfortunately what will happen is people will just use Google Maps instead so Microsoft will lose out. 

Monday, April 07, 2008

Mapping house buying

I really like the user interface provided by Globrix to look at houses for sale, in particular the Google Maps integration. It's certainly much better than the UI of Right Move, although Property Bee adds immense power to Right Move and doesn't currently integrate with Globrix. One thing missing from Globrix is the ability to save properties, but apparently that will be coming along shortly.

So if we ever move house, finding a new one should be much easier than in the past.

Sunday, April 06, 2008

Control DVD

Imagine trying to convince your other half to go and see a film about a late 70s post-punk band from Macclesfield whose lead singer killed himself. Oh, and it's in black and white. I didn't even make the effort, so I waited for the DVD and bought that instead.

I grew up with New Order but Joy Division had always been that other band that transformed into New Order. It's only over the last few years that I've grown to appreciate their music more.

So, onto the film. We all know how it ends (and if you don't, you really should read more, like the start of this blog entry) so there's no surprise there. But given the myth that's grown up around Joy Division and more specifically Ian Curtis, it's perhaps surprising that he's portrayed as a mostly normal bloke, who just had serious problems coping with his increasing fame, epilepsy, fatherhood and his own infidelity. Fact is I think anybody would have trouble coping with all of that. He's also shown as not treating his wife particularly well either so rather than a troubled genius, he comes across as a pretty normal flawed human.

It pays to watch it at least a couple of times. When I watched it the first time I was constantly waiting for him to have a fit or to attempt suicide, rather than actually watch the film. And the film is worth watching since it's beautifully filmed (as you'd expect from Anton Corbijn) and pretty entertaining to boot.

I think the reason we are so drawn to musicians who die young is the thought of what they might have achieved had they lived and for Ian Curtis it's particularly true. For me their three greatest songs are 'Love Will Tear Us Apart', 'Atmosphere' and 'Ceremony', all written pretty close to his death and all sounding pretty damn good even now. So just as they were approaching greatness, he died. But saying that, the fact he died when he did may just add to the beauty in those songs...

Then again, if he had continued on, we almost certainly know how the story would have ended. Joy Division may have produced some more great songs and albums and become as big as U2 (as Peter Hook thinks), but at some point they'd have got old and crap, just like New Order did. So though his death must have been painful for his family and friends, perhaps it was best for us music fans.  

Friday, April 04, 2008

Credit crunch, what credit crunch?

With all this talk of a credit crunch, I'm somewhat confused. Isn't the requirement to have some kind of evidence of an income, a deposit and a clean credit history before being given a mortgage actually a return to the lending practices that were the norm just a few years ago? Now, having a pulse is not the only requirement to be given huge sums of money. Rather than a credit crunch, isn't what we are seeing a return to normal service? And that seems like a good thing to me.

Thursday, April 03, 2008

What I hate about London

I'm not sure at what point I switched from enjoying living in London to wanting to escape as soon as possible. It was probably a slow process but I'm definitely in the latter camp now. Here are a few of the reasons...

Too expensive - For sure you can get paid more if you work in London, but this is completely wiped out by the cost of living here. Ridiculous house prices, expensive travel costs, over-priced pub and restaurant prices all add up to a fortune.

People - I don't have a problem with the people as such, although of course people up North are friendlier, but there are just too many of them. There is nowhere to go to get away from everyone, although deep within the bowels of Wimbledon Common can be pretty quiet on a weekday.

The other problem is that a lot of the people here are only here temporarily, generally to earn a bit of cash before heading off somewhere else. It's hard to have a sense of community when your neighbours are constantly changing.

Driving - I remember when I was younger I used to enjoy driving but the reason I enjoyed It was because I was able to drive without thinking too much. Driving in London is a different matter. First you have to constantly look out for idiots who are going to pull out in front of you. Then you get harassed by some idiot who is determined to get to wherever they are going in as short a time as possible. Then when you get to where you're going to you have find somewhere to park, generally an almost impossible task.

Dysfunctional weather - I have happy memories as a kid of building igloos, sledging and putting chains on the Land Rover when the snow came down. In London I have memories of having a super quick snowball fight and trying to knock together a snowman before all the snow melted by lunchtime. The ground temperature is so high, snow has no chance of surviving. Other weather works kind of as expected I guess.

Commuting - Fortunately I don't have to suffer the pain of this anymore, boy am I glad about that.

Wednesday, April 02, 2008

SCOPE_IDENTITY() and @@IDENTITY

Note to self, always use SELECT SCOPE_IDENTITY() instead of SELECT @@IDENTITY. I knew this already but always forget which one I should be using.

http://codebetter.com/blogs/john.papa/archive/2006/04/07/142503.aspx

Tuesday, April 01, 2008

A world without money lenders

It is becoming increasingly clear that the UK's housing boom wasn't caused by a lack of supply and heightened demand, but rather by slack lending practices by the banks. The proof can be seen now that cheap credit has been taken away and house prices have almost immediately started to drop.

The question this makes me ask is what would happen if there were no mortgages? Presumably houses would be cheaper, essentially costing whatever they cost to build plus some kind of profit for the builder. And wouldn't we all be richer, since we wouldn't need to service such big debts? OK, we'd all have to save up some money to buy a place, but this would be easier since renting would also be cheaper, given that landlords would have a smaller outlay to buy a place and hence would be happy with a lower rent to still get a decent return.

OK, this is all something of a simplification of the economics, but it sure seems like things would be better. I can foresee a few problems. Banks would have to start lending money to other people, since their whole business model requires them to lend money in order to make a profit, but then they could be providing finance to companies who actually generate wealth, rather than to pay for some bricks and mortar.

And even if we wanted to get to this Utopia, we wouldn't want to start from where we are. There'd be a lot of unhappy people with mortgages who would see the value of their houses plummet (me included).

So it might not work, but there seems to be something fundamentally wrong with the current situation. We have got progressively richer but as we have, house prices have increased in line with our increased wealth, thus swallowing up any extra cash. Where's the sense in that? Wasn't the idea that we'd get wealthier so that we didn't have to work so hard?