Friday, April 27, 2007

Manually creating an HttpRequest for testing

I'm currently writing unit tests for some code that uses the information found in the Headers property of an HttpRequest object. As such, I want to be able to create my own HttpRequest object with the appropriate headers.

This isn't as easy as I'd like it to be, as presumably the intent for the class is that you're handed an instance of it at the appropriate time, rather than putting one together yourself (which is fair enough).

Time to delve into System.Reflection for a bit of hacking:Job done!

Tuesday, March 20, 2007

UpdatePanel still performing full postback

Had some fun tracking this one down today, fortunately someone else had already done the hard work in working out how to fix it.

Essentially I was trying out a basic AJAX page in an existing 2.0 web application project. The various additional required sections were added to the web.config and I followed the Introduction to the UpdatePanel control, where a label is updated when you click a button.

Simple. Or so you'd think. No AJAX behaviour for me - it was performing a full postback.

Fortunately I managed to find a post from "Rick" that explained what the culprit was - the <xhtmlConformance mode="Legacy"/> in the web.config. Commenting this out gives the desired behaviour.

I must admit I haven't bothered investigating any further to work out why that works - feel free to let me know.

Found another post about this that explains a bit more from Scott Guthrie.

Tuesday, March 06, 2007

Selecting random records from a database table

A little nugget I picked up from my colleague Wim today, that was actually his first blog post a few years back.

Need a random sample of data from your database? Try this:


Friday, March 02, 2007

DeflateStream and zlib headers

The DeflateStream class in System.IO.Compression, as the docs state, represents the DEFLATE algorithm as in RFC 1951.

What it doesn't do (out of the box) is support compressed data in zlib format (see RFC 1950). When trying to decompress existing data, the easiest way I've found to get round this is simply to get rid of the 2 byte zlib header and use what's left, so something like the following works nicely if you've already got the data as a byte array in memory:

Array.Copy(zlibBytes, 2, strippedBytes, 0, zlibBytes.Length - 2);

Then just use the strippedBytes with DeflateStream instead.

Out of interest, I also tried stripping off the 4 byte Adler-32 checksum footer, and DeflateStream worked happily with that too.

Friday, February 23, 2007

Defining values in enums

I got caught out earlier with something not working as I expected. It may well be documented behaviour somewhere, but I've not been able to find anything specifically mentioning it following a brief search, so I thought I'd document it.

In an attempt to follow the guidelines for declaring an enum, specifically, placing the values in alphabetical order and providing a default "zero value", I came up with the following:

public enum Decision {
  Unknown = 0

Unfortunately this leads to the following values being assigned:
Accept = 0
Error = 1
Reject = 2
Unknown = 0

and thus Accept and Unknown become one and the same.

Make sure that any that are declared with a value go above any that aren't!