Archives / 2008 / August
  • ASP.NET MVC Preview 5 Released

    Hey, I'm just helping to spread the word! A sampled a few links and quotes that has already been posted to blogosphere for your pleasure and knowledge. :)

    Download it here ->

    Phil Haack said:

    We didn’t originally plan to have another preview. However, we implemented a few significant chunks of functionality and were dying to get feedback so that we could incorporate it into the product before Beta. It helps that with five or so of these interim releases, we’ve become pretty efficient producing these releases.

    We plan to have our next release be our official Beta, which means we’ll have a lot more test passes to produce and run before we release the next one.

    Some preview 5 related blog posts:

    Brad Wilson wrote about changes to partial rendering and view engine in preview 5.

    Maarten Balliauw wrote about easier form validation with preview 5.

    Nick Berardi has a list of news and changes and also a few issues he's found and submitted bug reports for:

    Derik Whittaker had a few comments regarding sealed classes in preview 5:

    It looks like that they removed the sealed keyword from many of the Attributes such as HandleErrorAttribute, AuthorizeAttribute and various other existing Attributes. 

    However, looks like many of the other attributes (some new, some not) such as AcceptVerbsAttribute, ModelBinderAttribute and NonActionAttribute are still marked as sealed.  Guys, please unseal all your stuff.  If you have a very, very, very valid reason then fine seal them.  But if not, let developers loose and unseal them.

  • Auto Postback with Javascript in ASP.NET MVC

    I've looked this up twice now so I'm posting it to my blog for future reference and as a quick-tip for others. Say you got a web page with a dropdown/select listbox and you want to reload/auto postback the page when the selection changes.


    One way to do this without involving a Javascript or Ajax library like jQuery (which probably is a good idea anyway :) is to use the "htmlAttributes" parameter of the Html.DropDownList() helper method and add a "onchange" attribute through an anonymous type. Something like this:

        Select a name: <%=Html.DropDownList("Name", ViewData.Model, 
    new { onchange = "doSomeJavascript()" })%>

    To submit a form, for example:

        <% using (Html.Form<HomeController>(p => p.DropDown(), 
    FormMethod.Post, new { id = "myform" })) {%> Select a name: <%=Html.DropDownList("Name", ViewData.Model,
    new { onchange = "document.getElementById('myform').submit()" })%> <% } %>


    The minimal DropDown() method of the HomeController class to support this sample looks like this:

    public object DropDown()
        var list = new List<string> { "Adam", "Bob", "Charlie" };
        return View(new SelectList(list, Request["Name"] ?? list.First()));

    As you can see, the htmlAttributes parameter is available on many of the Html-helper methods and I'm using ot to add a name attribute to the HTML form as well.

    End Note About Basic Knowledge of Javascript and Html

    Heh, ASP.NET MVC sure forces you to dust off that old, basic knowledge of HTML and Javascript that I think every web developer should have but the ASP.NET programming model has made us forgot... One could argue that it's niggy gritty stuff that we shouldn't have to worry about, but for me it feels good to know I'm in full control of the generated HTML and I'm not sending one byte more on the wire than what's needed. Yes, it's possible to have the same control with standard ASP.NET applications and controls, but I've seen experienced developers make mistakes around this more than once. ViewState anyone? :D

  • Returning Json from RESTful Interface with WCF

    WCF2 Someone commented on an earlier blog post I did on REST, POX/POJO and WCF and the comment read:

    How about REST WCF bits from .NET 3.5 SP1? Is it possible now to let the user decide in which format he wants the response (xml or json) like MySpace API for example?

    The convention is to use a file like extension at the end of the resource to specify data return type (.xml or .json)

    UPDATE/EDIT: Turns out I was doing this the hard way as there is support for json serialization right from the ServiceContract which makes this extremely easy. Just make sure to specify the ResponseFormat to be json. In a previous "version" of this blog post, I used the JavaScriptSerializer class, which is... dumb :)

    First go take a look at the sample that Kirk Evans had on his blog.

    Note that it may be easier to create a RESTful interface with ASP.NET MVC if you're into that tech, but that's another blog post.

    So, first I'm modifying the REST interface somewhat, adding support for /details.xml and /details.json URI:

    public interface IService
        Customer GetCustomer(string id);
        [WebGet(UriTemplate = "customers/{id}/details.json", 
    Customer GetJsonCustomer(string id); }

    As you can see, on the GetJsonCustomer() method, I'm specifying the ResponseFormat to be json. That's it :)

    A sample implementation for this interface looks like this:

    public Customer GetCustomer(string id)
        return new Customer { ID = id, Name = "Demo User" };
    public Customer GetJsonCustomer(string id)
        return GetCustomer(id);

    Using Fiddler to simulate client request and see what comes out of our RESTful service, we get this result from the /customers/123/details.xml request:

      <Customer xmlns="" xmlns:i=""><ID>1</ID><Name>Demo User</Name></Customer>

    ...and this from the /customers/123/details.json request:

      {"ID":"123","Name":"Demo User"}

  • Why Developers Are Interested in REST

    I had a laugh when I saw James Kovacs' blog post abot Why Developers are Interested in REST:

    I'm doing some WCF work for a client, specifically around WS-Security. I stumbled upon the System.ServiceModel.MessageSecurityVersion class today. Its static properties alone should explain why developers are craving simpler technologies like REST...

    I think he's got a point there actually. Web services with WCF can be very easy to use, and it can be so very complex it'll get your head to spin. The REST and POX/POCO style of services is definitely getting more attention, even from within Microsoft. WCF now supports RESTful interfaces, and we've seen REST-related demos by well known Microsoft presenters several times now, especially around WCF, EF and Astoria/ADO.NET Data Service. Also, it's very easy to create a RESTful service with ASP.NET MVC. We saw several REST-related presentations at Mix07 and Mix08 and I'm sure there will be a lot of REST/POCO/POX/Atom demos at PDC later this year.

    Ofcourse there will be a number of situations and type of applications which require a more complex protocol than what REST can offer, but as a developer and software architect who prefers simple solutions, I just love the simplicity of it all. Or maybe I'm just old and smilingly recognize old techniques coming back again - we did XML on (S)HTTP with ASP years and years ago, didn't we? And it worked pretty darn well I can tell you ;)

    The "REST effect" is perhaps the result of the WS-* race, which in my opinion exceeded the speed limit a long time ago. It's just impossible for an average human beings to keep up with it unless you want to become a WCF/WS expert and do nothing much else, which is not an option for me. I know, there are people out there, like my colleague Eric, who groks it all, but he's not human ;)

  • SyncToy 2.0 Released

    image Version 2.0 of SyncToy has been released on MSDN Downloads. SyncToy does not replace FolderShare for syncing files between different computers, but is a more effective way to copy/move/sync files on your computer. The dev-team uses the tool primarily to move pictures and music from devices (cameras, mp3 players etc) to folders on their computers or network shares in a more effective way. It's not a service/system tray kind of thing, just a utility that remembers a number of different "syncs" you've setup so that you can run them again and again when you need to.

    The download page says:

    SyncToy, a free PowerToy for Microsoft Windows, is an easy to use, highly customizable program that helps users to do the heavy lifting involved with the copying, moving, and synchronization of different directories. Most common operations can be performed with just a few clicks of the mouse, and additional customization is available without additional complexity. SyncToy can manage multiple sets of folders at the same time; it can combine files from two folders in one case, and mimic renames and deletes in another case. Unlike other applications, SyncToy actually keeps track of renames to files and will make sure those changes get carried over to the synchronized folder.

    The tool has got a number of new features added from version 1.0, and I'm listing a few interesting ones:

    • Dynamic Drive Letter Assignment
    • True Folder Sync
    • Exclusion Filtering Based on Name
    • Filtering Based on File Attributes
    • Command line enhancements: Added the ability to manage folder pairs via the command line interface.
    • Sync Encrypted Files
    • 64-Bit Support

    SyncToy is built upon the Microsoft Sync Framework, which seems to get better and better all the time. Worth keeping an eye on for everyone involved in developing applications which requires replication or off-line modes.

  • The Touch Wall Demonstration by Bill Gates

    This video has been around for a few months now, but it's still cool. It's not one of Bill's better demos as he's kind of standing in the way of the screen all the time, but it still shows off the Touch Wall/Surface capabilities pretty well. I'm sure we'll see something like this in class rooms and larger meeting rooms in the future. It's easy to learn to use, but the presenter will have to learn how move on the podium as well as work with the presentation and give the talk. Interesting... what do you think?




    The video sequence is from the Microsoft CEO Summit 2008.

  • Excel Automation - Example with Named Ranges

    excel And now to something completely different... the Excel automation. Every now and then you need to open up an Excel file and do stuff with it. In this case we needed to create and read from named ranges in Excel, and it's not overy easy to get information about how to do that, so I thought I might as well post some sample code here.

    To get started with Excel automation, there is a good KB-article on MSDN:

    Now, here's a console app in .NET 3.5 using VB.NET (I prefer to work with VB.NET with Office Interop and VSTO), which opens Excel, adds a new workbook, creates a (3,5) string array with values, fills the a range with these values, creates a named range and gets the values again to see that it worked properly.

    Imports Microsoft.Office.Interop
    Module Module1
        Sub Main()
            Dim application = New Excel.Application
            application.Visible = True
            Dim workbook = application.Workbooks.Add()
            Dim worksheet As Excel._Worksheet = workbook.ActiveSheet
            Dim saRet(3, 5) As String
            For iRow = 0 To 3
                For iCol = 0 To 5
                    saRet(iRow, iCol) = iRow.ToString() + "|" + iCol.ToString()
                Next iCol
            Next iRow
    'get a range, alternative 1 'Dim range As Excel.Range = worksheet.Range("A1:E3", Reflection.Missing.Value) 'get a range, alternative 2 Dim range As Excel.Range = worksheet.Range("A1", Reflection.Missing.Value) range = range.Resize(3, 5) 'set value of range to that of the string array range.Value = saRet 'name the range, explicitly (using dollar $ sign) workbook.Names.Add("NamedRange", "=$A$1:$E$3")
    'clear range range = Nothing 'get the values of the named range range = worksheet.Range("NamedRange") Dim values(,) As Object = range.Value Console.WriteLine("rows:" & values.GetUpperBound(0)) Console.WriteLine("cols:" & values.GetUpperBound(1)) Console.WriteLine("value of row 2, column 4 (D4) = " & values(2, 4)) Console.WriteLine("Press key to exit...") Console.Read() workbook.Close(False) application.Quit() End Sub End Module

    Hope this helps someone.

  • Totally Off Topic - Swedish Rune Stones From the Viking Age

    Ölsastenen This is totally off topic from the things I usually write about, but I thought I might show some pictures of a few beautiful rune stones we have scattered around not far from where we live (Botkyrka, southern part of Stockholm, Sweden) and tell you something about this particular type of landmark the Vikings left us. My knowledge of rune stones and runic inscriptions are minimal and I may get some info slightly wrong here, so bear with me :) I'm sure Wikipedia got tons of info on the topic if you hunger for more.

    The typical rune stone is a raised stone with runic inscriptions on it, but it also happens that you run across runes inscribed on bedrocks. It seems that the main purpose of these stones where to mark important events, explain inheritance, boast about the construction of a bridge or a road and very often to bring glory to kinsmen who travelled far and (quite often) died in foreign lands. Most rune stones are located in Scandinavia (Sweden, Norway, Denmark, Iceland), but there are also a few in locations where the Scandinavians of that time (mostly Norsemen) went, like England, Greenland and Ireland.

    What's so cool is that most of these stones were carved and erected during the Viking Age (from around year 790 to 1100), and thousands of these stones still stand where they were erected more than a thousand years ago. Some stones have been moved from where they were first found to a place close nearby where they are more accessible to folks who are interested in them. Through the years stones have been destroyed by construction, stolen by collectors and damaged by air pollution but still there are over 6.000 registered rune stones or fragments of them around, most of them (over 3.000) are located in Sweden, and most of those in the province of Uppland on the eastern coast of Sweden, just north of Stockholm.

    In Sweden, most of the stones are kept in a healthy state and looked after by a state organization. These people remove lichen and sometimes paint the runes with red paint, which is believed to be the most common color used. There are stories of runes being reddened by blood, but that's probably just that - stories. The rune stone above to the right is called "Ölsastenen", found in Uppland but moved to Skansen, an open air museum and zoo located in the middle of Stockholm. It is painted in (probably) authentic manner. That picture I took with my mobile camera, so the quality is quite bad.

    If you ever visit Stockholm and want to see a few rune stones, Skansen is probably the easiest place to go.

    The alphabet used on most of stones around where we live is the one called The Younger Futhark which consists of 16 characters:

    younger futhark

    Some experts are so skilled in this language, they can read it right off the stones if the runes are clear enough to read. What may be interesting to know is that the English language borrowed hundreds of words from the language spoken by the Vikings of this age, Old Norse. The Scandinavian words were introduced during the Viking invasion of England in the 9th and 10th century. It's easy to recognize some of the words written on the rune stones in both the English and Swedish language. For example, the Viking word faþur, means father which is fader in Swedish. There are many, many other examples of old, basic words which are similar in English and Swedish. Now, I think that Old English and Old Norse were derived from the same Germanic language, so it may be that the word father came from there, but it's still cool. I've read somewhere that Scandinavian "businessmen" visiting the British islands could make themselves understood quite well, even without screaming and raised swords...

    Most of the rune stones in the Uppland area have a Christian cross carved into them, showing they were of the Christian faith or supported a Christian king.


    This picture I took with a somewhat better camera and show the stone at Hamra (Sö 289). It reads "Björn and Holm--- erected this stone after Kättilbjörn, their --- God help the soul". Some carvings cannot be clearly read as you can see.

    Uringe, Grödinge

    This is the stone at Uringe (Sö 298) and reads "Håur and Karl and Sighjälm and Vihjälm and Kåre erected this stone after Vigmar, their father". How nice of them! The stone is over 1000 years old.


    To get an understanding of the size of some of these stones, here's a picture of the stone at Uttran (Sö 305) and myself. The picture is taken by my son Pontus and a rune stone excursion we did earlier this summer. This stone was erected by two brothers and reads "Sibbe and Tjarve erected this stone after Torkel their father".

    Glömsta Glömsta

    The pictures above show the bedrock carvings in Glömsta (Sö 300), just 5 minutes from where we live. This rune stone is a bit special because it's dedicated to a mother: "Sverker built the bridge after Ärengunn, his good mother". The bridge Sverker mentions is probably not a bridge over water, but rather improvement of the (what is believed to be an important) road that passed by this very place. So to honor his mother, perhaps he inherited lands from her, Sverker had this road improved and let everyone that used it read about this. 1000 years ago. I know, it's not like wall paintings in pyramids, but still cool.

  • My Toolbelt

    ToolbeltMy friend Jan-Erik told me he was about to write a blog page about good tools he is currently using, so I thought I might just do the same. I like my machine clean, so if I'm not using a program or tool for some time, it's out. I'm not listing the most obvious tools and programs from Microsoft (word, vs etc.), but these tools I use frequently and they help me do my work as a developer and software architect, so in no particular order:

    Windows Live Writer - a wonderful tool from Microsoft for writing and managing your blog posts. I'm using it now and will probably use it for a very, very long time. It's that good.

    Cropper - a superb screen capture utility by Brian Scott. It's small, neat and looks great too :)

    µTorrent - a pretty good bit-torrent client that just works.

    Fiddler - web debugger by Microsoft (written by Eric Lawrence I believe) that will help any developer who's into AJAX, REST, ASMX, WCF, plain HTML or whatever project that involves sending stuff over HTTP.

    Paint.NET - not as good as Photoshop, but not far from it and free! Written in .NET, this wonderful program gets new features every week it seems. Was #19 on PC World "Top 100 Products of 2007".

    Foxit Reader - a light weight, free, alternative to Adobe Reader for reading (and annotating) pdf files. I wish I could replace all programs from Adobe like this, man how I hate the extra junk Adobe installs on my machine. I just want to be able to read PDF files... same call goes to HP while I'm at comapnies that install "things" that bogs down the startup of my laptop.

    Gmail Notifier - I don't use Gmail alot, sometimes I'm not in there for over a week or two, so a notifier is required or I'll miss some important message. This notifier is not great, but it works (most of the time). Thinking of boiling my own notifier or just jack Gmail into Outlook...

    TortoiseSVN - the "The coolest Interface to (Sub)Version Control". I use this to get open source projects, like IronRuby and ASP.NET MVC. The Windows Explorer integration is really nice.

    TrueDownloader - open source download manager I've been using for a while now. As with uTorrent above, there are loads of download managers out there, but this one works and has no banners or ads.

    FolderShare - from Microsoft, and it's one of the coolest free tools you can get your hands on. Keep files in sync across your computers, share folders with friends and access your files from any computer. And it works :) If you haven't used this program yet, give it a try. I use it to backup source code and documents between 3 computers.

    Daemon Tools - 'nuf said!

    Developer Specific Tools and Addins

    I thought I'll go through the more developer specific tools and addins in a separate list, so here goes:

    Expresso - a powerful tool to help you write regular expressions. Requires a registration code after 60 days, but it's free.

    NUnit - one of the most frequently used unit test frameworks for .NET out there.

    PostSharp - an aspect oriented programming "weaver" for .NET which plugs in to visual studio and MSBuild. PostSharp is a great tool for doing AOP weaving in .NET, if you need it that is. Read up on AOP before you go there, most times there are other options.

    ReSharper - the number one addin to VisualStudio ever written. If you're a c# or developer, this is the one tool you should learn to use. There is so much to say about this work of art from the JetBrains, but it makes you way more productive and it helps you become a better programmer.

    GhostDoc - addin for VisualStudio by Roland Weigelt which tries to automatically generate XML documentation comments. No block buster, but it's cute :)

    Moq - a mocking library for .NET which takes good advantage of .NET 3.5 with linq- and lambda expressions. My mock library of choise!

    PowerShell and PowerTab - if you're a powershell user and also a .NET developer, make sure you install powertab by Marc van Orsouw (the powershell guy), which is an extension to powershell that gives you tab-expansion for the .NET library. To get an understanding of the power (heh) of this, check out this DNR-TV episode with Scott Hanselman.

    Reflector - aw, you know this one already! This is a now legendary tool by Lutz Roeder.

    Note: the toolbelt image is a leather toolbelt from Charles & Hudson that I just thought looked great

  • More on RESTful Service with WCF and POX/POCO

    Kirk Eveans wrote a blog post about Creating RESTful Services Using WCF, which gives you a good understanding of how to get started with REST on WCF. In his sample, Kirk has 2 methods in a REST interface:

    public interface IService
        [WebGet(UriTemplate = "customers/{id}")]
        Customer GetCustomer(string id);
        [WebInvoke(UriTemplate = "customers")]
        Customer PostCustomer(Customer c);

    The Data Contract for Customer looks like this:

    [DataContract(Namespace = "")]
    public class Customer
        public string ID { get; set; }
        public string Name { get; set; }

    Kirk also describes how to use the Fiddler tool to send REST request to the service, which is a wonderful tool for these circumstances.

    Now, to get the customer with ID 123, just send a GET request to the url: and the service will return:

    <Name>Demo User</Name>

    To call the other method, PostCustomer(), send a POST request to with the following request body:

    <Name>Demo User</Name>

    This returns:

    <Customer xmlns:i="">
    <Name>Hello, Demo User</Name>

    NOTE: Remember that you must add a HTTP header, specifying the content type, or the POST request will fail (Content-Type: application/xml).

    Wrong Order of Nodes?

    But what if the programmer sends the <Name> node before the <ID> node? Like this:

    <Name>Demo User</Name>

    The service will then return:

    <Customer xmlns:i="">
    <ID i:nil="true"/>
    <Name>Hello, Demo User</Name>

    Note that the ID is null! If the ID was declared as an integer in the Data Contract, the response from the service would be:

    <Customer xmlns:i="">
    <Name>Hello, Demo User</Name>

    Note that ID is 0 (zero), which could become a somewhat hard bug to catch. I asked Kirk about this and he confirmed the reason for this behaviour is the way the DataContractSerializer works. If no order is specified in the DataContract, it will (de)serialize in alphabetic order. If this is a problem for you, there is away around it by specifying the XmlSerializerFormat attribute on the REST interface:

    public interface IService
        [WebGet(UriTemplate = "customers/{id}")]
        Customer GetCustomer(string id);
        [WebInvoke(UriTemplate = "customers")]
        Customer PostCustomer(Customer c);

    POCO Support in SP1

    One of the new features in .NET 3.5 SP1 is the support for POCOs - the DataContractSerializer supports serializing types that doesn't have the [DataContract] or [Serializable] attributes. Aaron Skonnard has a good post on this. This means you can safely get rid of the attributes on the Customer class:

    public class Customer
        public string ID { get; set; }
        public string Name { get; set; }

    Note that the DataContractSerializer is still picky about the XML it gets to be able to deserialize it properly. Again, to get a more "relaxed" REST interface where WCF accepts the Name and ID nodes in any order, use the XmlSerializerFormat. I'm not sure this is what you want, but it's an option.

    I had a short mail conversation with Kirk about this, and he raised an interesting question about the lack of a industry accepted standard for describing RESTful services and I think he's right there. The XML-node order wouldn't be a problem at all if I gave the client programmer a schema or a contract which specified exactly how the RESTful interface was to be accessed and in which order the XML-nodes must come. Kirk had a lot to say about this, and I do hope he writes up a blog post about his thoughts ;)

    There are ways to send and receive any XML to a RESTful interface with WCF, and I'll write a blog post about that another day.

  • WPF is Different - The XAML Way of Doing Things

    WPF Wow, I've finally spent some time looking at Silverlight and WPF samples, and it sure takes a while to wrap your head around "The XAML Way of Doing Things". It's so easy to fall back to the WinForms coding style when you're supposed to do it The XAML Way.

    For example, if you have an options dialog with user settings for your app - DON'T do it the old WinForms way, but look at Configuration with Databinding (from Scott Hanselman's adventure with BabySmash). The code behind you need is really minimal if you do it the right way.

    There are also a gazillion ways to handle control events declaratively within the XAML itself, without having to create a code behind event and code things up from there. Take a look at WPF Styles and Triggers and learn it! Especially if you want to create nice looking effects, animations and such, but styles and triggers are useful for more than bells and whistles. For many things related to the UI there are 2 ways of doing it - in XAML or in code behind. There's a pretty good starter on Styles and Triggers on the Microsoft Belgium MSDN site. It may not be wrong to do it in code behind, and some things *must* be coded that way, but always search for The XAML Way of doing it before falling into old WinForms behaviour.

    Finally, what will take me a long time to understand and learn (if I ever learn it) is the XAML layout manager. I'm trying to position a group of controls in the center of a WPF Window that may be in full screen mode. First I used the Canvas panel and hacked away in a WinForms style specifying location depending on screen size and stuff, but I'm sure it can all be done in XAML... somehow *smile* ScottGu wrote an intro for Silverlight 2 on the layout manager, which is good reading if you are new to this.

    I should probably install Expression Blend and learn at least the basics of it. The XAML editor in VS2008 is just not enough for more complex layouts. What scares me somewhat is that a fairly complex WPF/Silverlight window may result in pretty nasty XAML with a bunch of Grids, Stackpanels, Canvases, more Stackpanels within Stackpanels within a certain Grid.Column and so on. Add a bunch more if you're using triggers and animation. I know you can shrink the XAML somewhat by using Styles, but are we getting back into HTML Table Hell again when we've just been saved by CSS?