Perfil de PaulioIT BytesBlogListasScraps Ferramentas Ajuda

Blog


31 de março

The wacky world of parallel SQL

I was asked to look a SQL Server problem where a single user was running a single batch query and getting a deadlock, which on the face of it is a pretty neat trick. The error stated; 'was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim'. Why piqued my interest was the phrase 'communication buffer'. The query was pretty complex with a large number of joins and some casts on the join conditions but I was reassured that the code had worked fine on the development servers just not on the pre-live server. So what is a communication buffer in this context? I made a leap and assumed it was where the plan has split the work into its various stages and the communication was where the data was been processed and combined as the plan is executed. But still why would that cause a deadlock...ah well the title of the blog probably gives it away ;). The pre-live server is a pretty big beast with more cores than the average orchard and with only one user (no stress) it was likely that SQL was going to attempt to use a number of them. So in the great tradition of using a hammer to crack to a nut we switched the degree of parallelism on the server to be 1 rather than 0, i.e. don't do it. The query ran fine. I probably should report this as a bug with SQL 2005 but if you do run into this problem then I'd suggest you use the maxdop hint in your query...or turn it off at the server, afterall what other nasties could this feature cause?
 

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!
 
21 de março

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);}}
 
12 de março

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.
 
 
09 de março

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.
05 de março

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();
        }