The wacky world of serializers

 Recently I’d set my debug exception handling to catch pretty much every exception thrown and I kept seeing a rather strange error…
System.IO.FileNotFoundException occurred
  Message="Could not load file or assembly myType.XmlSerializers"
 
What was odd about this error is that although the type in question was being serialized it wasn’t overloading any serialization code, so why the exception? The question did the rounds of the development team and a colleague discovered the truth. It turns out that as an optimization .net searches for a special optimized serialization version of the type, based on the type’s names. If it finds it, it uses it. If not a file not found exception is raised and then automatically handled and the default serializer is called into play. What I find odd about this concept is that in order to use an optimization, that I would have to write anyway, the framework makes an expensive disk access followed by an exception. So the effect is that if I don’t implement the optimization I get an extra performance hit!
 

Silverlight Digg search example

I’ve decided to follow Scott Guthrie’s Silverlight example for creating a Digg Search application. It’s a nice example covering a number of topics, however…there are a few gotcha’s (problems) in it.
  1. The tutorial involves a number of swaping one element type for another, the compiler can get easily confused. You can get weird errors that reference the old element when it is nowhere to be found in the source code. It seems that stepping though the initialize code seems to solve this!??
  2. The data control in the tutorial is called Data:DataGrid, for me it’s my:DataGrid
  3. Part of the tutorial explains how to separate style from structure, however a number of the controls the tutorial uses don’t support styles as resources. However, if you download the sample code you’ll see that Scott has replaced those with more basic versions, for example the tutorial uses a WatermarkTextBox (doesn’t support resource styles) whereas the sample code uses a straight forward TextBox.
  4. The tutorial shows the application displaying images, and indeed it does show how to bind to the thumbnail image. However, neither the tutorial or the sample code actually displays images. The problem seems to be that the Linq query is trying to store an entire Xml element into a string, doesn’t work. So you need to change the Linq query from

    Thumbnail = (string)story.Element("thumbnail"), to Thumbnail = (string)story.Element("thumbnail").Attribute("src"),

  5.   Binding the Hyperlinkbutton directly to the string href of the story class doesn’t work, it needs to be a URI. So add a URI property to the story…

public Uri HrefUri{get{return new Uri(this.HrefLink);}}

 

Silverlight 2 gotcha

I was going through an example from Microsoft’s Tim Sneath and hit the following error…
A first chance exception of type ‘System.Windows.Markup.XamlParseException’ occurred in System.Windows.dll
Additional information: AG_E_PARSER_BAD_PROPERTY_VALUE
 
I dread this sort of error in Silverlight, but it turns out that the answer in this case was fairly simple, if a little suprising. The example has 3 slider controls each sharing the same ValueChanged event handler. However, as the page is initialized the slider values are firing their ValueChanged events. Unfortunatley the shared event handler examines the value of all the controls but they are not yet available, hence the error. To avoid this I simply check for null in the event handler.
 
 

How to read docx (word 2007) files when you don’t have Office

I wanted to read a docx file today but I don’t have any Office products (or equivalent installed) so the trick is to;
1. Install Word 2003 viewer (download from MS site)
2. Install Office 2007 compatibility kit (download from MS site)
3. Reboot (required for me so the docx extension was correctly registered

The next trick is that when I double click the document the converter from the compatibility kit kicks in but then the dumb thing launches WordPad instead of the 2003 viewer. So currently I’m forced to open the viewer first and browse to the docx.

Silverlight 2.0 (beta)

I’d picked up Silverlight 1.1 pretty quickly and initially got stuck into to solving its pretty close to non-existent keyboard support. However, other time factors got involved and although my solution worked ok it was always going to be a poor substitution to the real deal. So it was with renewed enthusiasm that I downloaded the latest incarnation, VS2008 templates and all. Unfortunately it’s taken me most of the day to get my development environment installed (requiring a number on uninstalls of previous beta sdks) but so far the wait seems to have been worth it. The first thing that strikes you is the far more mature project templates in VS2008 with creation choices including hosting the project in a new web site or via a HTML page. The latter is interesting because it hides the page…although the former is the weapon of choice if you want to avoid all those annoying ‘enable javascript’ browser prompts. Next things is you get split designer view ala Expression Blend. Although my early experience with the design surface is pretty…well pointless. More of an instant preview than a UI surface, I’m sticking with the XAML for now. The next thing that stuck me is the lack of all the supporting files, e.g. javascript, manifests, etc. Well VS has kindly hidden those, or rather bundled them up in another one of the ever common zip-thats-not-a-zip-but-is-only-renamed files, much like the Office docs. Still that’s great for me, since I do have some idea what they are and probably won’t want to have to interact with them like I did before. Obviously the well publicised changes are there, all the lovely WPF like controls albeit without any glossy 3D. What did strike me as unusual is that coding the page is still a bit…odd. For example, the steps I went through to put a caption on a button (without RTM first) were…
1. Drag button onto UI surface – Nope nothing
2. Drag button onto XAML – yes
3. Properties of button – nothing
4. Click in button tag to bring up intellisense – yes
5. Look for something like text or caption – nothing
6. Enter text in the ‘innertext’ of the tag – not allowed
7. Examine every possible attrib’ via intellisense – tried ‘content’ and that worked
8. Looked for name – no
9. Looked for x:name – yes, still using the x namespace

Ok, so if you read any of the SDK you’d figure it out, but I was interested to see if the approach had become…obvious, it hasn’t. For me it still remains the most cumbersome of the developer experiences but that’s probably more due to the improvements elsewhere against the relatively immature interface for Silverlight. I realise there is a lot more to think about but it still fills a bit clunky. I’d certainly like to see VS giving me member fields for controls rather than me having to code that manually via FindName. Still, I’m very excited to start some Silverlight projects in earnest, I’ve a couple of ideas, so hopefully…watch this space for some demos.

Testing ASP.NET Session state part 2

After publishing the first example I thought I’d better show how to check the actual values in the cache too…
       
static void Main(string[] args)
        {
            Page page = new Page();
            MockManager.Init();
            MockObject sessionMock = MockManager.MockObject(typeof(HttpSessionState));
            Mock pageMock = MockManager.MockAll(page.GetType());
            pageMock.ExpectGet("Session", sessionMock.Object);
            sessionMock.ExpectGetIndex("ValueInIndex").Args("IndexKey");
            string value = (string)page.Session["IndexKey"];
            Console.WriteLine(value);
            Console.ReadKey();
            MockManager.Verify();
            MockManager.ClearAll();
        }

Testing ASP.NET session cache

I was asked how to test the ASP.NET Page.Session object. I’m sure there are many ways of doing it, but here is my quick console sample using TypeMock;

 static void Main(string[] args)
        {
            Page page = new Page();
            MockManager.Init();
            MockObject sessionMock = MockManager.MockObject(typeof(HttpSessionState));
            Mock pageMock = MockManager.MockAll(page.GetType());
            pageMock.ExpectGet("Session", sessionMock.Object);
            sessionMock.ExpectGetIndex("bert");
            string value = (string)page.Session["bert"];
            Console.ReadKey();
            MockManager.Verify();
            MockManager.ClearAll();
        }

Remote debugging in Visual Studio 2008

A quick post about a couple of potential gotcha’s with remote debugging in VS2008.

First off, you need to use the remote debugging monitor that ships with VS2008, although they look identical the VS2005 monitor won’t work with VS2008. Next problem is where to find the monitor. In VS2005 it was a separate install on the VS2005 setup disks, in VS2008 it’s installed into the common IDE folder under VS2008 (…Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Remote Debugger\x86). You simply copy the files from there to the machine you want to monitor. As for getting it to use breakpoints and the like then it’s back to the old tricks of remote debugging. Basically to make life easy it’s much better to just rebuild the DLLs you’re interested in and ship them to the target…saves a lot of hassle.

Visual Studio Addins not showing

I’ve developed a small Add-in for Visual Studio and kept running into a rather odd problem. During the testing of the addin it would work fine, I’d see my Addin in the addin manager and I’d get a couple of new menu items showing under the tools menu. However, when I built a release version, copied the files into my VisualStudio 2008\Addins folder I could only see the Addin in the manager but not the menu items. Turns out that I just needed to get rid of the XXXFor Testing.Add file from the Addins folder. Well I renamed it to make it easier to debug later, but I certainly confused me for a while, I think the debug version should have a different name, which you can do by editing the .addin file.
 

Portal

I really enjoyed playing Portal by Valve but I was even more happy to complete it. The other day I was made aware of this Flash version http://www.newgrounds.com/portal/view/404612, at last I completed that too…but did I get any cake in the end, well that would be telling.