WCF WebSockets: First Glance

I finally got around to playing with the first drop of WebSockets support for WCF. I’m pretty familiar with WebSockets as I’ve been using Node.js to play around with them for quite a while now. The server API in Node.js is wonderful as it couldn’t be simpler – to demonstrate this, take a look at how we’d create a basic echo server.

var ws = require("websocket-server");

var server = ws.createServer();

server.addListener("connection", function(connection){
  connection.addListener("message", function(msg){


When I heard that Microsoft was planning on integrating WebSockets into WCF my first thoughts weren’t all that positive. Now I know it’s not quite the case since .NET 4, but WCF has had a reputation for rather large complex API’s and heaps of XML. I was dreading what a WCF take on the above would look like. Turns out, perhaps I should be a little more optimistic.

class Program
    static void Main(string[] args)
        var host = new WebSocketsHost(new Uri("ws://localhost:4502/echo"));

public class EchoService : WebSocketsService
    public override void OnMessage(JsonValue jsonValue)

That’s really the kind of simplicity I was really hoping to see. Now the interesting question is how it’s going to look when dealing with multiple clients. Node kind of has an advantage here as it’s entirely single threaded (yeah, I said advantage), but with .NET’s recent concurrent collections and a rather nice API for dealing with events (Rx) I’m feeling pretty hopeful.


What I’d like to see in C# 5

Lately my work has made me quite the polyglot. Although I’m still in C# most of the time, I’m also writing bits of Groovy, Ruby, Java and of course JavaScript because I’m that cool. There are many things to like about C# – LINQ, lambdas, extension methods, anonymous types, etc… All of these things make C# a wonderfully productive language for the applications that I write. There are however a few features I get to use in other languages which I feel could make wonderful additions.

String Interpolation

A simple one to start with. Python, Ruby and Groovy let you use code expressions inside strings which are immediately evaluated.

var dave = new Person { Name = "David" };
var welcomeText = "Hello ${dave.Name}!";

So you’re probably thinking that this isn’t all that different from just using string.Format(“Hello {0}!”, person.Name) and fine, there’s really not much difference. But I’d argue that having these kind of things built into the language really help with general productivity. Miguel de Icaza of Mono fame also happens to agree with me and has even proposed a workable syntax to support it.

Range Indexing

We’re all familiar with indexing arrays – you give it a number and it returns the item at that position. Other languages take this much, much further providing powerful indexing abilities.

var a = { 1, 2, 3, 4, 5 };
var secondItem = a[2];
var lastItem = a[-1];
var middleItems = a[1..3];
var everyOtherItema = a[0, 2, 4];

Again these things are nothing that can’t already be achieved by using existing functionality – LINQ provides a lot of this. However these are operations we do so often – why not add language support for them?

Regular Expressions

I’m a big fan of regular expressions. If they’re not one of the tools in your belt then you’re missing out big time. The FCL already provides a fairly quick way of testing strings against a regular expression.

RegEx.IsMatch("^/d+$", "123")

Ruby among others goes a step further and bakes this right in the language. I’d like to see C# make regular expressions a primitive type like a string or an integer. Then maybe offer operators to make testing values against them more succinct.

var regEx = /^[0-9]+$/;
var isNumber = "23" =~ regEx;

Actually now I look at the operator I’m not entirely sure if I like it, but it could be a nice idea.

Grander Things

There are some bits of Ruby that make me very jealous. I keep thinking to myself that Mixin’s and some of the incredible meta-programming techniques could really help writing reducing some of the pain I find writing ViewModels for my WPF and SL applications. How would this integrate into C# I don’t know. Of course with IronRuby officially out the door perhaps just sticking with Ruby is an option. Expect to see more thoughts from me in that direction in the future.


I feel that the above would make C# slightly more productive, but would it be worth the extra complexity? Perhaps not. I’m pretty sure none of us want to end up with something like Perl.

More importantly, from the rumors coming out of Redmond it sounds like the big focus of C# 5 will be exposing the “compiler as a service”. I’d rather have something like the awesome pluggable/expendable pipeline that powers Boo in C# over these rather minor enhancements.