Archives / 2008 / April
  • Silverlight - The Song

    About 6 months ago my good friend Spike Xavier and I got together on a Saturday to work on a new song since we like to get away from programming now and then and mess around with music for fun.  It'd been awhile since we released our last song titled No More DLL Hell and we decided that we needed to write a new song about one of our favorite new application frameworks....Silverlight.  After a few hours we came up with some patterns and riffs we liked but we weren't happy with the overall melody.  We shelved it, got busy with other work things and didn't do much related to music. 

    A couple of weeks ago Spike came over and we started up the project again and finished the initial version of the song.  I'm a perfectionist (which can be really bad with song writing and recording) so I tweaked it over the next few days adding background layers, experimenting with different drum kits, synth sounds, mix levels, effects, etc.  We sent out an initial beta of the song (yeah....we're adding geek terms into the process of song writing) to Scott Guthrie, Adam Kinney and Tim Heuer (all cool guys directly involved with Microsoft's Silverlight product) and a few others but we still wanted to change a few things.  After playing around with the song a little more and finding "the" drum kit that we both liked for the intro we got it all wrapped up.  It's different than a "standard" song since we wanted to keep it short and sweet with an intro that builds up.

    So, without further adieu, here's the world premier of our new song titled Silverlight.  Some will love it, some will hate it....and that's music.   Add a comment either way and let us know what you think. Thanks in advance to all 6 of our fans (including my wife and kids and Spike's wife and kids), to Live Streaming for hosting it and to Silverlight guru Tim Heuer for reminding me about Live Streaming. :-) 

    Update:  We've had several people inquire about using the song in talks, podcasts, etc.  You're free to use the song in any (positive) manner.  A reference to where people can download it (to this blog entry) would be appreciated. 

    Download MP3 (right click and Save As) 

    Spike Xavier and Dan Wahlin (with additional help from Heedy Wahlin)


    feed this need for quality
    vector based delivery
    we write it in xaml, we roll like a caml
    we love it like baml
    3D dioramma

    lightweight subset of the CLR
    down the wire with fluidity
    you won't believe it, if you can conceive it
    your users retrieve it
    and their minds receive it.

    it's Silverlight
    it's strong and tight
    they got it right
    try it tonight

    take the web to a brand new height

    No More DLL Hell (MP3)

    Spike Xavier and Dan Wahlin

    Here's some other stuff I've done:

    Story Of Your Life (MP3)
    Reality (MP3)
    No Shame in Crying (MP3)

    Finally, here's a list of the equipment and software used since I know a lot of other developers like to play around with music as well: Tags: ,,


  • Pushing Data to a Silverlight Client with Sockets: Part II

    In Part 1 of this two part series on socket support in Silverlight 2 I discussed how a server could be created to listen for clients using classes in the System.Net.Sockets namespace.  In that post the TcpListener class was used to listen for client connections and the client stream was accessed using the TcpClient class's GetStream() method.  In this post I'll cover how a Silverlight client can connect to a server using sockets and receive data pushed by the server asynchronously.

    Creating a Silverlight Socket Client

    Silverlight 2 provides a Socket class located in the System.Net.Sockets namespace (a Silverlight namespace...not the .NET Framework namespace mentioned earlier) that can be used to connect a client to a server to send and/or receive data.  The Socket class works in conjunction with a SocketAsyncEventArgs class to connect and send/receive data.  The SocketAsyncEventArgs class is used to define asynchronous callback methods and pass user state between those methods (such as the Socket object that originally connected to the server).  An example of using the Socket and SocketAsyncEventArgs classes is shown next:

    private void Page_Loaded(object sender, RoutedEventArgs e)
        DnsEndPoint endPoint = new DnsEndPoint(Application.Current.Host.Source.DnsSafeHost, 4530);
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        SocketAsyncEventArgs args = new SocketAsyncEventArgs();
        args.UserToken = socket;
        args.RemoteEndPoint = endPoint;
        args.Completed += new EventHandler<SocketAsyncEventArgs>(OnSocketConnectCompleted);

    This code first identifies the application's host server along with a port of 4530 (Silverlight can only connect to servers on a port between 4502 and 4532).  A Socket object capable of communicating using the TCP protocol is then created.  Once the Socket object is created, a SocketAsyncEventArgs object is instantiated and the socket object is assigned to the UserToken property so that it is passed through to other methods.  The target endpoint of the server is set using the RemoteEndPoint property and the argument object's Completed event is hooked to an asynchronous callback method named OnSocketConnectCompleted.  Once these objects are created and ready to use, the Socket object's ConnectAsync() method can be called which accepts a SocketAsyncEventArgs object as a parameter.

    When the client is connected to the server the following method is called which creates a buffer to hold data received from the server and rewires the SocketAsyncEventArgs's Completed event to a different callback method named OnSocketReceive:

    private void OnSocketConnectCompleted(object sender, SocketAsyncEventArgs e)
        byte[] response = new byte[1024];
        e.SetBuffer(response, 0, response.Length);
        e.Completed -= new EventHandler<SocketAsyncEventArgs>(OnSocketConnectCompleted);
        e.Completed += new EventHandler<SocketAsyncEventArgs>(OnSocketReceive);
        Socket socket = (Socket)e.UserToken;

    The Socket object's ReceiveAsync() method is then called to begin the process of accepting data from the server.  As data is received the OnSocketReceive() method is called which handles deserializing XML data returned from the server into CLR objects that can be used in the Silverlight client. 

    private void OnSocketReceive(object sender, SocketAsyncEventArgs e)
        StringReader sr = null;
            string data = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
            sr = new StringReader(data);
            //Get initial team data
            if (_Teams == null && data.Contains("Teams"))
                XmlSerializer xs = new XmlSerializer(typeof(Teams));
                _Teams = (Teams)xs.Deserialize(sr);
            //Get updated score data
            if (data.Contains("ScoreData"))
                XmlSerializer xs = new XmlSerializer(typeof(ScoreData));
                ScoreData scoreData = (ScoreData)xs.Deserialize(sr);
                ScoreDataHandler handler = new ScoreDataHandler(UpdateScoreData);
                this.Dispatcher.BeginInvoke(handler, new object[] { scoreData });
        catch { }
            if (sr != null) sr.Close();
        //Prepare to receive more data
        Socket socket = (Socket)e.UserToken;

    The deserialization process is handled by the XmlSerializer class which keeps the code nice and clean.  Alternatively, the XmlReader class could be used or the LINQ to XML technology that's available in Silverlight.  Notice that data is routed back to the UI thread by using the Dispatcher class's BeginInvoke() method.  If you've worked with Windows Forms before then this should look somewhat familiar.  That's all there is to work with sockets in a Silverlight client.  There's certainly some work involved, but many of the complexities are hidden away in the Socket and SocketAsyncEventArgs classes which both minimize the need to work with threads directly.  I've already shown what the client interface looks like as data is updated but here it is one more time (click the image to see an animated gif of the application in action):

    The complete code for the Silverlight client and server application can be downloaded here.


  • Pushing Data to a Silverlight Client with Sockets: Part I

    Silverlight 2 has built-in support for sockets which creates some interesting possibilities.  If you've ever worked on a client-side application that needed to receive up-to-date data then you're probably used to solving the problem by polling.  With polling the client contacts the server on a consistent, timed basis to see if any updates are available.  ASP.NET AJAX provides a Timer control that makes this process easy and straightforward.


  • Silverlight 2 Networking Options

    image I've been spending my nights working on a new book covering Silverlight 2 and have been focusing on the new networking features that are available in the System.Net and related namespaces.  Silverlight's great at animating objects, performing transformations and collecting data with all of the new controls that are available, but at some point you'll need to retrieve data or send data to a service.  There's great support built-in for calling WCF services, ASMX services as well as other services.  Support for  calling REST APIs is also very good and easy to implement using classes such as WebClient and HttpWebRequest/HttpWebResponse.  Asynchronous requests can be issued and handled quite easily once you figure out the pattern (which is quite consistent throughout the different networking classes).


  • Using Silverlight 2 ItemsControl Templates

    Building Silverlight 2 applications reminds me of the first time I built an ASP.NET application.  There are so many new features and controls that it takes a little time to get up-to-speed with what's available and how it can be used.  It's definitely fun but at times you just want to throw the monitor out the window when something doesn't work like you think it should (disclaimer:  I've never actually thrown a monitor anywhere...but I'm definitely guilty of wanting to do it).  The good news is that the learning curve flattens out pretty quickly once you grasp a few key concepts which probably makes my monitor feel much better.  :-)


  • Video: Silverlight Rehab

    I just watched a new video called Silverlight Rehab that's pretty dang funny (heard about it through Adam Kinney who's on the Silverlight team).  Even my wife laughed which is saying a lot since she doesn't typically appreciate "geek" humor much.  I'm not sure who put it together or came up with the idea, but it's definitely creative.

    Update:  Adam let me know that Tina Wood came up with the idea.  A follow-up video needs to be made now to see how the rehab process is going.

    Silverlight Rehab - You're not alone


comments powered by Disqus