Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Dev Blog - Johan Danforth

I'm Johan Danforth and this is my dev blog - a mix of .NET, ASP.NET, Rest, Azure and some other random coding stuff.

  • 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: http://support.microsoft.com/kb/q302094

    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.

    Hamra

    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.

    Uttran

    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 vb.net 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:

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

    The Data Contract for Customer looks like this:

    [DataContract(Namespace = "")]
    public class Customer
    {
        [DataMember]
        public string ID { get; set; }
        [DataMember]
        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: http://127.0.0.1:8000/customers/123 and the service will return:

    <Customer>
    <ID>123</ID>
    <Name>Demo User</Name>
    </Customer>

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

    <Customer>
    <ID>123</ID>
    <Name>Demo User</Name>
    </Customer>

    This returns:

    <Customer xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <ID>123</ID>
    <Name>Hello, Demo User</Name>
    </Customer>

    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:

    <Customer>
    <Name>Demo User</Name>
    <ID>123</ID>
    </Customer>

    The service will then return:

    <Customer xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <ID i:nil="true"/>
    <Name>Hello, Demo User</Name>
    </Customer>

    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="http://www.w3.org/2001/XMLSchema-instance">
    <ID>0</ID>
    <Name>Hello, Demo User</Name>
    </Customer>

    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:

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebGet(UriTemplate = "customers/{id}")]
        [XmlSerializerFormat]
        Customer GetCustomer(string id);
    
        [OperationContract]
        [WebInvoke(UriTemplate = "customers")]
        [XmlSerializerFormat]
        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?

  • Open a WPF Window from WinForms

    If you find yourself in need to open a WPF Window from a WinForms program - this is one way to do it (works for me):

    1) Create/Add a new project of type "WPF Custom Control Library"

    image

    2) Add a new Item of type "Window (WPF)"

    image

    3) Do your thing with the WPF Window

    4) From your WinForms app, create and open the WPF Window:

    using System;
    using System.Windows.Forms;
    using System.Windows.Forms.Integration;
    var wpfwindow = new WPFWindow.Window1();
    ElementHost.EnableModelessKeyboardInterop(wpfwindow);
    wpfwindow.Show();

    There you go!

    The EnableModelessKeyboardInterop() call is necessary to handle keyboard input in the WPF window if loaded from a non-WPF host like WinForms. I understand there are other ways to do this and you can read more about WPF/WinForms interop here.

  • IronRuby with ASP.NET MVC

    MVCIronRuby Now that ASP.NET MVC preview 4 is out, Phil Haack did as promised and made available a working prototype of IronRuby + ASP.NET MVC integration. He wrote:

    Now that Preview 4 is out, I revisited the prototype and got it working again. I use the term working loosely here. Yeah, it works, but it is really rough around the edges.

    To be able to use Ruby with ASP.NET MVC there has to be some c# glue/bridge code, especially in global.asax, which loads the routing tables (via extensions) and points at a specially made Ruby Controller Factory:

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.LoadFromRuby();
        ControllerBuilder.Current.SetControllerFactory(new RubyControllerFactory());
    }

    That glue code is placed in a c# library called IronRubyMvcLibrary, which also contains support for .rhtml view pages. The objects you need to use from within the view page is accessible through Ruby global variables - $model, $response, $html and so on.

    As John and Phil points out, this is a prototype and a test to try out a few things with IronRuby and MVC, and I think it's cool. Much will change over time, like how they use instance variables on the controller to pass data to the view.

    This is a step towards something which may become quite popular in the Ruby community, and perhaps to Rails people who has to, or wants to, write web apps on the .NET platform in the future. For those that hasn't been looking at IronRuby yet and want to have some fun with this - note that you don't have any intellisense or anything lika that in Visual Studio yet.

  • Fetching User Details from OpenId via Attribute Exchange

    CropperCapture[4]Rob Conery did another excellent episode of his MVC Storefront series the other day where he ripped out his user membership code and replaced it with OpenId.

    Implementing the whole OpenId protocol yourself isn't necessary as there are some great libraries for you out there which makes it really, really simple to use and integrate with both WebFoms and A SP.NET MVC. Rob uses the DotNetOpenId library to authenticate the user but didn't show how to fetch user details like the user full name.

    Well, this is possible using Attribute Exchange which is a way of transferring information about the user between the OpenID provider and relying party. The DotNetOpenId library supports this too, and it just requires a small change to the DotNetOpenId code samples I've seen out there.

    First, most people redirect to the provider like this:

    openid.CreateRequest(id).RedirectToProvider();

    But instead, create the request manually and add extension requests to fetch user information (like the name for example) to it before redirecting:

    var openidRequest = openid.CreateRequest(id);
    var fetch = new FetchRequest();
    fetch.AddAttribute(new AttributeRequest("http://schema.openid.net/namePerson"));
    openidRequest.AddExtension(fetch);
    
    openidRequest.RedirectToProvider();

    Just keep on adding the attributes you're interested in, like email, website etc. Remeber to inform your user about you wanting to fetch the extra information before doing the authentication.

    After a successful OpenId authentication the provider redirects back to your site and you normally authenticate and redirect like this:

    switch (openid.Response.Status)
    {
        case AuthenticationStatus.Authenticated:
            FormsAuthentication.RedirectFromLoginPage(openid.Response.ClaimedIdentifier, false);
    //...snip...

    Now, before the redirect statement, you have the chance to fetch out the attributes you asked for from the OpenId response, like this for example:

    switch (openid.Response.Status)
    {
        case AuthenticationStatus.Authenticated:
            var fetch = openid.Response.GetExtension<FetchResponse>();
            if (fetch != null)
            {
                //assuming we got data back from the provider - add sanity check to this :)
                var name = fetch.GetAttribute("http://schema.openid.net/namePerson").Values[0];
                //...store name in session or something...
    }
    FormsAuthentication.RedirectFromLoginPage(openid.Response.ClaimedIdentifier, false); //...snip...

    Note that DotNetOpenId library offers a few "well known" attributes as enums which all uses the "axschema.org" schema, but they do not work with the myOpenId provider. That's why I'm using the "schema.openid.net" schema instead.

    Rakuto Furutani blogged about the attribute schema problems in a Ruby on Rails post:

    myOpenID supports only some attributes that can be exchange with OpenID Attribute Exchange, but you should care about Type URI, because myOpenID doesn't support Type URI described on "http://www.axschema.org/types/". You should specify Type URI with "http://schema.openid.net/" instead.

    It could be I'm using an older version of the DotNetOpenId library and it's been changed in newed releases. I have to check this out.

    Also, Andrew Arnott has written a longer post on how to use DotNetOpenId's Attribute Extensions extension which you should read if you want some more in-depth sample code (he's using ASP.NET WebForms).

  • ThreadPool.QueueUserWorkItem with Anonymous Types

    I thought this blog post by Matt Valerio was good, and it gave me a few ideas to use in a current test project. He wrote a helper method to be able to use anonymous types when calling a delegate or lambda in the ThreadPool:

    public delegate void WaitCallback<T>(T state);
    
    public static class ThreadPoolHelper
    {
        public static bool QueueUserWorkItem<T>(T state, WaitCallback<T> callback)
        {
            return ThreadPool.QueueUserWorkItem(s => callback((T)s), state);
        }
    }

    And his example code for using this method:

    ThreadPoolHelper.QueueUserWorkItem(
        new { Name = "Matt", Age = 26 },
        (data) =>
        {
            string name = data.Name;
            int age = data.Age;
            // Long-running computation
        });

     

    Cute, eh?