Me Love You Runtime Code Monkey!

Static Content in IIS7

Everytime I start a new server install on IIS7 I forget one thing, I install IIS and its management console and also .net 3.5 and 4.0, but always forget to check that Static Content is checked.

This means that when I start my site, it generally works straight away, but no images or CSS or even javascript are shown, when I look at the response for these items, I get a 200, but the content is empty.

To rectify, just go to your server and in your Server Manager within the Roles area, in Role Services, select Static Content under the Common HTTP Features category and try again.

Rails on 64-bit Windows 7

Recently I have decided to try out Ruby on Rails again. Ok, I have done one or two projects with it, but I want to have another go. I’m going to share my thoughts here about it and what’s got me. After some time with C#, most error messages don’t phase me much anymore, however, Rails starts my learning again.

I’ve just been bashing the keyboard to bits in an attempt to get  Rails 3 to work on my 64-bit Windows machine. The error was that SQLite3.dll could not be found, even when I changed my config.yaml to look for postgresql.

To fix, download the sqlite-dll-win32-x[version].zip from the Sqlite website, then unzip to C:Windowssystem (not System32) and oddly it works.

I hope this helps someone from losing their hair :).

Cookie Versioning

Working on some major sites in the past. I have noticed one thing a lot of devs (including me) don’t do. Version cookies. We are taught as developers that code changes and we must make sure our code is easily maintainable because of that. Cookies tend to be forgotten though.

Take this example: We have a simple login for a site. When the user is logged in, some details like name are stored in the cookie and retrieved by the site. The client comes along and says “I want the user to also have their t-shirt size persisted in the cookie”. So we dutifully add this detail to the cookie. No issue there, we login to test and it works! The cookie is read and their correct t-shirt sizes are displayed. However, we don’t notice other users on the site might already have the old cookie and don’t log in again. They may well get an error as we expect the cookie to have the t-shirt size.

The solution is very easy though, you’ve probably guessed it already. If you have a key in your cookie called version and its value of 1 or whatever you want. If there is a change, we can then change the version to 2 in our configuration file. When a user’s cookie is read, we can then do a simple compare on the server, if they have a different cookie version or one which is less than the latest, we can delete the cookie, so they have to log in again, or just update it with the new details, or whatever behaviour you want your app to do as a reaction. This makes for a stronger cookie reading process.

Of course, this is an example, you’d probably have the user’s shirt size in the database and retrieve it with a call when the user logs in. But cookies can change with data. Sometimes, it is best to be ready for that from the start so you can be sure the user doesn’t get an error.

ByteArray to file

I work a lot with Flash developers (or actionscripters) and most of the time Flash will need to post a form to the server to process or something similar. A server side developer will then write some services to receive or return data to the Flash. Sometimes the data isn’t that standard. Sometimes the Flash will generate an image and will want to save it to the server. It will post a ByteArray to the server, this term scares some developers for some reason. It is just a raw file in a load of bytes. But it needs to be processed to become an image or other file again to be saved. This is how easy it is:

//Read byte array into
byte[] byteArray = [the byte array from the flash];
MemoryStream mStream = new MemoryStream();
mStream.Write(byteArray , 0, byteArray.Length);

//Save as PNG
Image image = Image.FromStream(mStream);
image.Save([Location to save file to], ImageFormat.Png);

In this example I take the byte array posted by the Flash and convert it into an image (PNG formatted). It takes the byte array and writes it to a MemoryStream. The image object then reads the MemoryStream object and saves it. Of course, the necessary checks will need to be done to make sure no nasties or malicious files are uploaded etc. but this is the crux of it and very simple.

[CSS3] System Selection

A new selector is available in CSS3. It goes something like: ::selection and it allows the user to change the highlight colour of the page. e.g.

.red::selection {
	background: #ff0000;
}

It’s a small step, but nice and it gracefully degrades (or progressively enhances, depending on your outlook on life) if the user is using an older browser.