Events in C# 4.0

Rx : Reactive Extensions for .Net and JavaScript.

Rx is a set of framework extensions to allow developers to easily do asynchronous programming via familiar interfaces. If you are familiar with LINQ and IEnumerable<T> and IEnumerator<T> you will instantly be able to grasp and use the new interfaces IObservable<T> and IObserver<T>. The enumerating interfaces allow you to PULL sequences of T from an interface in your code. The observing interfaces allow you to have sequences of T PUSHED to handlers in your code in response to asynchronous LINQ queries. Rx is implemented over the top of PFx (Parallel Framework extensions) so all the threading and concurrency is handled for you including synchronization contexts which is especially useful if you are programming UI. It is also particularly significant that the Rx extension methods include a FromEvent() method that allows you to attach handlers to a steam of events in a very fluid way.

If you want to know more watch the video on the bottom of this page where Rx author Erik Meijer explains it in 14 minutes.

If you want to see it in action have a look at this Channel9 video with Wes Dyer where he implements drag & drop from scratch using Rx in 6 minutes.

Also, checkout the Rx Team Blog for downloads, updates and samples.

Finally, back to Channel9 to look at RxJS for Reactive extensions for Java Script so the UI I mentioned above now includes web UI too. As Jeffrey Van Gogh says in the RxJS video, web programming is about “asynchronous stuff” but java script is an imperative language. It’s really hard to do good asynchronous stuff in java script. RxJS changes that it a big way.

Building from the ground up

I recently carpet-bombed my main work laptop and built it from scratch. Not something I do very often but the time had come. Here’s what went back into the box (the box being my trusty Dell D830) :

OS : Windows 7 Ultimate 64-bit

Productivity Stuff :

Development Stuff :

System Management Stuff :

Utils & Misc :

Observations :

  1. Wow ! That’s a lot of stuff. Now I remember why I don’t do that very often.
  2. Development stuff still outweighs everything else by far.
  3. Why do I still have an optical drive on this laptop? During all of the above I never once used a CD or DVD.
  4. When I started developing I used TextPad and a compiler and I hated IDEs. I still occasionally use TextPad but now I have two versions of Visual Studio with a zillion plug-ins. My daughter once asked me “What’s that game you play where all the words pop-up and change colour.” It took me a while to work out she meant Visual Studio 🙂
  5. I really only installed Snippet Compiler for old time’s sake as development seems to have stopped. I use LINQPad for that stuff now.
  6. Outlook 2010 is amazing.
  7. Visio 2010 drives me nuts because I have LIVED in previous versions of Visio way too long and now everything has moved. It’s like my house has been burgled by Feng Shui experts with OCD. I’m learning to live with it though 🙂
  8. When the hell is LiveMesh going to get out of Beta ?
  9. My beloved Paint Shop Pro disappeared up its own arse around the time it was acquired by Corel (coincidence ?) and since then I have stubbornly stuck to version 6 which is next to useless on Windows 7. Paint.Net is a worthy and FREE replacement. Nice one guys.
  10. I want to love SketchFlow, but I also want to beat it’s stupid head in with a brick.
  11. Reflector is still way cool and it now has a “PRO” version for £125 – OUCH ! Guess how many new cool features the free version will be getting from now on 😉
  12. Loving LINQPad. It has more or less replaced Snippet Compiler, SQL Management Studio and a bunch of other little utilities on my machine. Take the LINQPad Challenge today.
  13. Nearly half my development stuff is now for mobile. Scary 😮
  14. Previous problems with Security Essentials have not manifested themselves. Hmmm….

What price security ?

For the past couple of weeks I’ve been trying out Microsoft Security Essentials. This is Microsoft’s new, free anti-virus, anti-spyware and anti-malware tool. For a long time I’ve been using either NOD32 by Eset or Sophos as my AV of choice but I figured I’d give this a go.
 
My primary requirement of any tool like this is that I should not even know it’s there. I will, at a push, concede to having a single, small icon in the system tray but there had better not been any popups or nags or hellos or splash screens or any of that crap unless there’s an actual, real live virus emergency. And, it had better not interfere with the normal running of my machine or my productivity. I don’t ask much, see ? Both NOD32 and Sophos are excellent in all these respects. Completely reliable when you need them and virtually invisible when you don’t.
 
The machine I ran Microsoft Security Essentials on was my 2 year old work-horse laptop, i.e. the machine I use most of the time, most of the day. It’s a Dell Latitude D830 with a 2Ghz Core 2 Duo CPU, 4GB of RAM, a 200GB 7200 RPM disk and a wide screen 1680 x 1050 TFT driven by a 256MB NVidia Quadro NVS 140M graphics card. I am running a fairly clean build of Windows 7 which performs like lightening and is an absolute joy to use.  It is, in short, a pretty good laptop.
 
Microsoft Security Essentials behaved impecably to begin with. It was quick and easy to download, install and configure and it ran silently in the background without seeming to get in the way. But then the trouble started. Specifically, I noticed a problem with audio playback. It was most apparent with video and especially flash. What I was getting was frequent, irregular buzzes, blips and judders in the audio. This was happening in flash, video players and even in media players when I was just playing MP3s. In Windows Media Player I could actually produce the problem by resizing or moving windows around on the desktop while audio was playing.
 
I tried various configuration changes, killing as many other processes on the system as I could, reinstalling audio drivers etc but all to no avail. Needless to say Microsoft Security Essentials has now been uninstalled and the problem vanished instantly. Obviously this may be a very isolated case related to my specific setup but I’d be ineterested to know if anyone else has similar problems.

Windows 7 RTM…the adventure.

I couldn’t face doing another complete rebuild on my machine so soon after having done one for the Windows 7 RC. So when I got the RTM I did a bad thing. I upgraded. This is not officially possible but if you copy all the source files off the DVD, edit the file sources\cversion.ini and set the MinClient setting to the same as the RC build number (7100) it will work just fine…well almost. Here’s a list of the stuff that didn’t.

1) Windows Virtual PC 2007 SP1. This does not seem to work on Windows 7 as Windows 7 has it’s own spanking new virtualisation. So I uninstalled the old one and set about trying to configure the new one. A couple of things about this though.intelutil

The new Virtual PC on Windows 7 requires hardware virtualisation support, i.e. your processor must support this. Then, as this is almost invariably disabled in the computer BIOS, you have to get in there and find the setting to activate it. In my case – Dell Latitude D830 – it was pretty well hidden in with the POST settings. To find out if your Intel processor supports this you can download the Intel® Processor Identification Utility from here. Once installed and run, look on the CPU Technologies tab under Intel® Virtualization Technology. If it’s set to Yes, you’re OK.

adaptersettings2) If you are a developer and you develop for Windows Mobile and you have the Windows Mobile SDK installed with its device emulators you may have a nasty shock at this point. The emulator uses the Virtual machine Network Services driver that is part of the old Virtual PC to allow it to piggy-back on your network connections. Once you uninstall Virtual PC 2007 this is all over. The fix for this is fairly straightforward since although Virtual PC 2007 does not work on Windows 7 it’s virtual network driver does. So, before you uninstall Virtual PC 2007 make a copy of the  Microsoft Virtual PC\Utility\VMNetSrv directory. Once the uninstall is complete go into your network adapter settings, pick any adapter and view its properties. Click on the install button, select Service and click Add… You should then be able to reinstall the driver from the copy of the directory you made.

3) Windows Mobile Device Center (WMDC). I previously had WMDC 6.1 installed on the Windows 7 RC and it worked fine. Again, no such luck on the RTM. So, I uninstalled it as the RTM is supposed to come with its own WMDC installed by default. Uninstalling the old one however seemed to knock the new one for six. It eventually started working again after a couple of reboots so I’m not sure what happened there.

In fairness to the RTM it did warn me that these thing wouldn’t work during the setup. Guess I should have taken more notice 🙂

warning

Code Quality

The Rule of 8 : High Performance Scalable Systems

Wille Faler has an interesting article on his blog where he highlights 8 core principles for building high performance, scalable systems. It’s an extension to the list of Best Practices for Speeding Up Your Web Site, but Wille is focussing on general principles for any scalable system rather than just websites.

In short, we have :

  • Offload the database – Avoid hitting the database, and avoid opening transactions or connections unless you absolutely need to use them.
  • What a difference a cache makes – For read heavy applications caching is the easiest way offload the database.
  • Cache as coarse-grained objects as possible – Coarse-grained objects save CPU and time by requiring fewer reads to assemble objects.
  • Don’t store transient state permanently – Is it really necessary to store your transient data in the database?
  • Location, Location – put things close to where they are supposed to be delivered.
  • Constrain concurrent access to limited resource – it’s quicker to let a single thread do work and finish rather than flooding finite resources with 200 client threads.
  • Staged, asynchronous processing – separate a process using asynchronicity into separate steps mediated by queues and executed by a limited number of workers in each step.
  • Minimize network chatter – Avoid remote communication if you can as it’s slower and less reliable than local computation.

How not to scale

Speaking of scale there’s an interesting article here by Bart Smaalders of Sun Microsystems about performance anti-patterns. He has identified a number of these anti-patterns :

  • Fixing Performance at the End of the Project
  • Measuring and Comparing the Wrong Things
  • Algorithmic Antipathy
  • Reusing Software
  • Iterating Because That’s What Computers Do Well
  • Premature Optimization
  • Focusing on What You Can See Rather Than on the Problem
  • Software Layering
  • Excessive Numbers of Threads
  • Asymmetric Hardware Utilization
  • Not Optimizing for the Common Case
  • Needless Swapping of Cache Lines Between CPUs

Although it does appear to have been written from the perspective of “performance over all” and some sections (e.g. the one about layering) ignore that fact that the history of software development has been a gradual trade-off between performance and other equally important things like flexibility, maintainability, portability, modularity, productivity etc, it’s a short, well written article that makes interesting reading.

The keys to success

There is a list here of all the keyboard shortcuts in Windows. Wow ! That’s a lot of shortcuts. I’ve been using Windows forever and I’ll admit right now I only know a fraction of those.
Speaking of keyboards, there’s a great article here about the give and take in a software project, i.e. what developers should expect to contribute and what they should expect to receive in return. In short :

What can the project do for you?

First up we have a bunch of items along the lines of “I expect — maybe demand — that management will provide me with…”

  1. Two monitors
  2. A fast PC with a crapload of RAM
  3. Choice of mouse and keyboard
  4. A comfortable chair
  5. Quiet working conditions
  6. Internet unlimited
  7. Freedom to install software
  8. The best software
  9. Good coffee
  10. Sensible working hours
  11. Separate Environments for dev, qa and production

What can you do for the project?

But it’s not all “take take take”.

The other side of the equation are items along the lines of “I am committed to ensuring that these practices are performed…”

  1. Source Control
  2. Continuous integration
  3. Track bugs
  4. Unit testing
  5. Code analysis
  6. Continual peer review
  7. Peer training
  8. Keep yourself up to date
  9. Learn to Communicate with non-technical people
  10. Refactor!
  11. Passion
It’s a good list, but go read the full article as it goes into each item in more detail and with extra funny thrown in. File under “funny but true”.
Finally, check out Favrd. It’s a tracker that maintains a feed of all the tweets from twitter that are “yellow starred” or “favourited” (that is a thing, I swear) and ranks them daily by number of stars. It’s like cutting out all the mining and just being handed the gold.

Extending the WCF Stack

In my last post I created a custom serializer that could be injected into the WCF plumbing between the dispatcher and the service class that allowed us to debug errors that occurred during serialization. This operation behaviour could be applied to a method signature in the service contract :
 
[ServiceContract()]
public interface IMyService
{
	[OperationContract]
	[DebuggableSerializer]
	Thing GetThing(Flavour flavour); 
	
	[OperationContract]
	[DebuggableSerializer]
	Thing[] GetOneOfEach();
}
or to the implementation of that method in the service class :
 
[DebuggableSerializer]
public Thing[] GetOneOfEach()
{
	List<Thing> things = new List<Thing>();
	foreach(Flavour f in Enum.GetValues(typeof(Flavour)))
	{
		things.Add(this.GetThing(f));
	}
	return things.ToArray();
}
However, what if I wanted to apply this behaviour as a ServiceBehavior, ContractBehavior or EndpointBehavior rather than an OperationBehavior i.e. if I wanted all the operations in my service implementation or interface to use it and I don’t want to have to go through and add the attribute to every method ? How can I use my [DebuggableSerializer] attribute to decorate a service class, interface or even better, apply the behaviour to the service or endpoint using a config file so I don’t have to change my code at all ?
 
So, in this post I am going to extend my [DebuggableSerializer] attribute so it can be used on a service class or interface and I am going to extend the System.ServiceModel.Configuration.BehaviorExtensionElement to allow me to control and apply that behaviour from my config file to either a service or a specific endpoint.
 
My attribute in the previous example looked like this :
 
public class DebuggableSerializer : Attribute, IOperationBehavior
but now we want to use it as a ServiceBehavior, ContractBehavior and an EndpointBehavior as well as an OperationBehavior, so we’ll implement the IServiceBehavior, IContractBehavior and IEndpointBeahvior  interfaces :
 
public class DebuggableSerializer : Attribute, IOperationBehavior, 
	IServiceBehavior, IEndpointBehavior, IContractBehavior
IServiceBehavior has three methods for which we need to add implementations and IContractBehavior and IEndpointBehavior have 4 :
 
#region IServiceBehavior Members 

public void AddBindingParameters(ServiceDescription serviceDescription,
	ServiceHostBase serviceHostBase, 
	System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints,
	BindingParameterCollection bindingParameters)
{
	ReplaceDataContractSerializerOperationBehavior(serviceDescription);
}

public void ApplyDispatchBehavior(ServiceDescription serviceDescription,
	ServiceHostBase serviceHostBase)
{
	ReplaceDataContractSerializerOperationBehavior(serviceDescription);
} 

public void Validate(ServiceDescription serviceDescription,
	ServiceHostBase serviceHostBase)
{
} 

#endregion

#region IEndpointBehavior Members 

public void AddBindingParameters(ServiceEndpoint endpoint,
	BindingParameterCollection bindingParameters)
{
} 

public void ApplyClientBehavior(ServiceEndpoint endpoint,
ClientRuntime clientRuntime)
{
	ReplaceDataContractSerializerOperationBehavior(endpoint);
} 

public void ApplyDispatchBehavior(ServiceEndpoint endpoint,
	EndpointDispatcher endpointDispatcher)
{
	ReplaceDataContractSerializerOperationBehavior(endpoint);
} 

public void Validate(ServiceEndpoint endpoint)
{
}

#endregion

#region IContractBehavior Members 

public void AddBindingParameters(ContractDescription contractDescription,
	ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{ 
} 

public void ApplyClientBehavior(ContractDescription contractDescription,
	ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
	ReplaceDataContractSerializerOperationBehavior(contractDescription);
} 

public void ApplyDispatchBehavior(ContractDescription contractDescription,
	ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
	ReplaceDataContractSerializerOperationBehavior(contractDescription);
} 

public void Validate(ContractDescription contractDescription,
	ServiceEndpoint endpoint)
{ 
} 

#endregion
We also need to provide 3 overloads of the ReplaceDataContractSerializerOperationBehavior method, one that takes ServiceDescription, one that takes ServiceEndpoint and one that takes ContractDescription instead of OperationDescription. The method that takes ServiceDescription will call the method that takes ServiceEndpoint for each Endpoint and the method that takes ServiceEndpoint will call the method that takes ContractDescription and the method that takes ContractDescription will call the method that takes OperationDescription for each OperationDescription in its contract.
 
private static void ReplaceDataContractSerializerOperationBehavior(
	ServiceDescription description)
{
	foreach (ServiceEndpoint endpoint in description.Endpoints)
	{
		ReplaceDataContractSerializerOperationBehavior(endpoint);
	}
}

private static void ReplaceDataContractSerializerOperationBehavior(
	ContractDescription description)
{
	foreach (OperationDescription desc in description.Operations)
	{
		ReplaceDataContractSerializerOperationBehavior(desc);
	}
} 

private static void ReplaceDataContractSerializerOperationBehavior(
	ServiceEndpoint endpoint)
{
	// ignore mex
	if (endpoint.Contract.ContractType == typeof(IMetadataExchange))
	{
		return;
	}
	ReplaceDataContractSerializerOperationBehavior(endpoint.Contract);
}
So now we can take our DebuggableSerializer attribute and place it on the service class or interface :
 
[DebuggableSerializer]
public class MyService : IMyService
{ ... }

[DebuggableSerializer]
public interface IMyService
{ ... }
We now have an attribute that can be used to replace the serializer on a service contract or an operation contract or a service operation or on a whole service class or an endpoint of a service. The final step is to be able to add this behaviour to a service or an endpoint from within our configuration rather than in code. For this we need to create a BehaviorExtensionElement that will create an instance of our DebuggableSerializer behaviour. This is very simple :
 
public class UseDebuggableSerializer : BehaviorExtensionElement
{ 
	public override Type BehaviorType
	{
		get { return typeof(DebuggableSerializer); }
	}
	
	protected override object CreateBehavior()
	{
		return new DebuggableSerializer();
	}
}
Next, in our config file, we need to define this behaviour extension :
 
<system.serviceModel>
	<extensions>
		<behaviorExtensions>
			<add name="UseDebuggableSerializer"
				type="WCFLibrary.UseDebuggableSerializer, WCFLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
		</behaviorExtensions>
	</extensions>
	<!-- elided -->
</system.serviceModel>
To add this behaviour to a service we add the extension to the service’s assigned behaviour :
 
<system.serviceModel>
	<!-- elided -->
	<behaviors>
		<serviceBehaviors>
			<behavior name="MetadataExchange">
				<serviceMetadata httpGetEnabled="true" />
				<UseDebuggableSerializer />
			</behavior>
		</serviceBehaviors>
	</behaviors>
	<!-- elided -->
</system.serviceModel>
To add it to an endpoint we define an endpoint behaviour and add the extension to that :
 
<system.serviceModel>
	<!-- elided -->
	<behaviors>
		<endpointBehaviors>
			<behavior name="DebuggableSerializer">
				<UseDebuggableSerializer />
			</behavior>
		</endpointBehaviors>
	</behaviors>
	<!-- elided -->
	<service behaviorConfiguration="MetadataExchange" 
		name="WCFLibrary.MyService">
	<endpoint
		address="net.tcp://localhost:7000/MyService"
		behaviorConfiguration="DebuggableSerializer"
		binding="netTcpBinding"
		name="TCP_7000_MyService"
		contract="WCFLibrary.IMyService" />
	</service>
	<!-- elided -->
</system.serviceModel>

%d bloggers like this: