Skip to main content

Pulled Apart - Part III: Get on the bus!

onebit_26Note: This is part of a series, you can find the rest of the parts in the series index.

One of the aspects of Pull is that it had to be multi-threaded, because things like downloading a massive podcast shouldn’t lock up the UI. Threading has become pretty easy in .NET 4 thanks to things like PLINQ or Parallel Extensions. However cross thread communication hasn’t gotten easier in .NET 4.

My idea to solve this was to create a internal bus – which is just an implementation of the pub/sub pattern. A bunch of subscribers register with the bus for a specific message type and when a message is given to the bus, it passes it to the correct subscribers.

image 

First thing I did was create a simple singleton instance of my bus class:

internal class Bus
{
    private static Bus bus = new Bus();

    private Bus()
    {
        publisher = new Publisher();
    }

    public static Bus GetBus()
    {
        return bus;
    }        

This ensures that all threads get the same instance. Inside my bus class I implement the new .NET 4 IObserver/IObservable interfaces which gives me all the pub/sub magic. This is all internal to the bus class so that usage in my application is just with bus. For example the methods for registering a subscriber is, which hides the pub/sub concept completely.

public void Register<T>(DataAction actions, Action<T> method, Control control = null)
{
    publisher.Subscribe(new Subscriber<T>(actions, method, control));
}

public void Register(DataAction actions, Action method, Control control = null)
{
    Action<object> fakeMethod = value => { method(); };
    this.Register(actions, fakeMethod, control);
}

One of the options when registering a subscriber is you can is pass in a control, which I use for handling objects owned by other threads which makes it very easy to update the UI.

Broadcasting to all subscribers who have registered for a message type is handled by a very simple method:

public void Broadcast<T>(DataAction action, T data)
{
    publisher.Update(action, data);
}

public void Broadcast(DataAction action)
{
    this.Broadcast<object>(action, null);
}

To identify the type of message I am using an enum, which I do not feel too great about. The advantage of using enum’s is that there is no magic strings which the compiler can’t identify (i.e. if I mistype a message, the compiler tells me) and that I can use flags to broadcast multiple messages at once. However the downside of enum’s means adding a new message means editing the list of enum’s which isn’t so great.

Final Thoughts

Overall I am exceptionally happy with the bus as it solved so many problems I have had with multi-threaded applications and I think should be a standard in application design in future.

Pulled Apart - Part II: What? You're not using TFS!

Note: This is part of a series, you can find the rest of the parts in the series index.

onebit_26Some people see learning as a side effect of software development and others believe that all must be known upfront and so no learning occurs. The reality is that it learning during software development is a core part and you should be embracing it. One of the the things I have done with Pull is to host the code with CodePlex. CodePlex offers two ways to store source code – you can use TFS/SVN or you can use Mercurial. 

In the past I have always used TFS because I am comfortable there – it’s a tool I know well and I like to use it. However to embrace the learning for Pull, I decided it could be a Mercurial project. Which brought me to my first issue, I didn’t have any Mercurial tools so I went off to find a set I liked.

Being that I didn’t want to install anything, because my machine is so light and fast, a lot of the packages out there were not an option (TortoiseHG, I’m looking at you). In the end I choose the Mercurial Cmd Portable from PortableApps.com which gave me a light option, but did mean no GUI which really isn’t a bad thing.

Comparing it to TFS is a two part compare.

  1. If I compare to the full GUI TFS inside Visual Studio then I prefer the Mercurial experience because working disconnected from the server and coming online is easier.
  2. There are TFS Power Tools which provides a command prompt tool called tfpt.exe and that has an online option which makes the whole experience just as good what Mercurial offers.

One of the big areas of difference between how TFS and Mercurial work is branching/merging/labels/forks etc… which I have not experienced yet.

Some of the things I did in my Mercurial setup which may help other people.

.hgignore

My ignore file for C# projects done in Visual Studio, with CodeRush installed.

syntax: glob

*.csproj.user
*/[Oo]bj/*
*/[Bb]in/*
*.suo
*DXCore.Solution
hgignore[.-]* 
[Tt]humbs.db 
*.user
*.bak.* 
*.bak 
*.[Cc]ache
*/[Pp]ublish/*
*.vssscc
*.vs10x

hgrc

My config was fairly straightforward, I have an auth setup so that my CodePlex details are remembered and I created an alias called codeplex. This lets me just type hg codeplex and it prompts for my password and that’s it – one line gets the repro pushed to CodePlex. I also setup WinMerge as the diff tool, because I am a WinMerge fan and this just lets me keep using it easily.

[auth]
codeplex.prefix=hg01.codeplex.com/pull
codeplex.username=rmaclean
codeplex.schemes=https

[alias]
codeplex=push https://hg01.codeplex.com/pull

[ui]
username=Robert MacLean <[email protected]>

[extensions]
; must uncomment this line
extdiff =

[extdiff]
cmd.winmerge = E:\PortableApps\WinMergePortable\App\WinMerge\WinMergeU.exe
opts.winmerge = /r /e /x /u /wl

Pulled Apart - Part I: Introduction

Note: This is part of a series, you can find the rest of the parts in the series index.

onebit_26I have needed software which the available implementations of that type of software doesn’t solve (due to cost, features, experience etc…) thankfully I like to write code and I like to share. This means I am often writing small applications to solve problems. My current one is a podcatcher, it is a program which downloads podcasts, called Pull.

pull

Pull is designed with the idea of being just a podcatcher, because all the media players out there with bundled podcatchers are either VERY heavy or just crap, so my solution to that is just be a tool which does one thing, and does it well.

The second major design feature is that it must be portable – assuming .NET 4 is installed then it should just run without an install. Smile

Lastly it should be quiet and just get on with the job of pulling down podcasts. I do not need to be annoyed with pop-ups and sounds all the time. My view is that I will deal with you when I have the time, else sit in the corner and do your job.

I thought I would blog a series of some aspects of the development both code and technology as I have learnt a ridiculous amount during the initial development. Check the series index at the top for a preview of what I have in mind or to find more parts in this.

Visual Studio and/or Test Manager corrupt licensing?

Blue Male Doctor In A Lab Coat, Sitting On A Stool And Bandaging A Blue Person That Has Been Hurt On The Head, Arm And Ankle Clipart GraphicAt the Visual Studio & TFS event we had a few machines complaining that the Test Manager license was invalid and a new one was needed. Those same machines also said Visual Studio’s license was corrupted and that Visual Studio needed a re-install.

To make this more odd, we were using virtual machines so every machine was identical yet only some machines had this problem.

The cause was the host OS date was wrong (the year was 2008) and so the virtual machines were set to 2008. In the eyes of the virtual machine this meant that the license was installed magically in the future.

We turned off the VM, deleted the state, fixed the date and started again and it was solved!

Wrapping up the VS & TFS hands on labs event

EventBanner

A recent Saturday (31st July), we ran a free day of Visual Studio 2010 and Team Foundation Server 2010 hands on labs. This event was run at the Bytes training facilities in Midrand, who set us up with 50 machines for people to use. BB&D jumped in and helped out with snacks (and giving me and Zayd time during work to plan this).

Together with Brent, from Bytes, and Zayd we ran around helped people out and got a ton of setup done! The crowd was fantastic (we had 70% to 80% attendance) with loads of hallway conversation about everything from licensing, Microsoft and even Visual Studio!

We were very honoured by three attendee’s who flew up from Cape Town to come to this event! This really highlighted the value of this event and we were all proud of it.

We also recommended people bring their own laptops to copy from the Microsoft Community Drives which was a massive success. We only had two drives so there was a queue for them. One of my personal highlights was walking past someone copying from the drive I saw them using one of my quick reference posters as a desktop wallpaper Smile 

Lastly, and definitely not least in value, was Devexpress gave us two Coderush licenses to raffle away and those were a great success and congrats to our two winners.

Thanks to everyone who attended and keep watching we have some ideas expand on this and are looking at running it again.

Resolving "Could not load type Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider"

If you are using AppFabric and decide to swop out the ASP.NET standard caching with it you may run into the error:

Could not load type "Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider”

The error will be pointing to the type of the custom session (line 5 below):

<sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider">
    <providers>
        <!-- specify the named cache for session data -->
        <add name="AppFabricCacheSessionStoreProvider" 
             type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider" 
             cacheName="TailSpin" sharedId="TailSpinTravelId"/>
    </providers>
</sessionState>

The cause is that the application can’t find that class. To help it find it you need to add the following to the Configuration –> System.Web –> Compilation –> Assemblies:

<add assembly="Microsoft.ApplicationServer.Caching.Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add assembly="Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

Why I like Visual Studio 2010? Undock Windows

imageVisual Studio 2010 has the ability to undock code windows by clicking and dragging on the code window tabs. This is a great feature and it is often touted for multi-monitor setups where you may want to have the form on one window and code on another window, or maybe two different code files open at the same time.

image

That is very useful, however that is not why I like this feature. I like the ability to undock the windows because it lets me view two different parts of the same file at the same time. Visual Studio 2010 and earlier have supported this using the split view option (image) but that means you lose half of your screen (like below).

image

Using the undock windows in 2010 you can use the Windows –> New Window option to duplicate the current window.

imageimage

Then you can drag the second (or third, or forth) windows out and get a full screen side by side view which is GREAT for comparing code.

image

Cannot open VS project if Blend is used?

1I ran into an issue recently after reinstalling my laptop, where I couldn’t open a C# (WPF) project in Visual Studio 2010. Every time I tried it just grimaced at me and said:

Unable to read the project file 'Rule18.csproj'.

E:\Projects\Rule18\Rule18\Rule18.csproj(335,3): The imported project "C:\Program Files (x86)\MSBuild\Microsoft\Expression\Blend\3.1\WPF\Microsoft.Expression.Blend.WPF.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

Part of the cause is that previously I had Blend installed, and I had used it on this project to do some fairly complex things, in particular Font Embedding. However since reinstalling I had not installed Blend again.

This is caused the build target file not to be installed, and that caused the error.

How do you get around this?

The best solution is to install the FREE Blend SDK because that will put the build targets on the machine. Links for the SDKs:

However I didn’t have time to do this, so as a temporary solution (and definitely not a recommended solution): I opened the project file, and right near the bottom I found the Import for CSharp and Blend. I removed the Blend one, saved and reloaded and worked.

2

VirtualBox UUID already in use

I am playing around with using VirtualBox to run virtual machines and I ran into an issue trying to use a clone (copy) of a VDI file. The VDI file is the hard disk drive and it has a unique identifier in it it (UUID) and so trying to use a clone of one gives you the error: UUID of the hard disk is already exist in the media registry.

To solve it you need to run the following command to change the UUID of the file:

VBoxManage.exe internalcommands sethduuid [VDI file]

Example:

"c:\Program Files\Oracle\VirtualBox\VBoxManage.exe" internalcommands sethduuid

AppFabric.vdi