Calender
<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

**UPDATED**

Since my previous post I have a couple of updates for my presentation at the Edmug meeting this month.

Date:  Due to room bookings, it will now be March 25th (Wednesday) @ 5:30 p.m.

Topic: As per a request from Don Belcham, I will be now presenting on Silverlight with MVVM.  I’m going to dive in to the MVVM (Model-View-ViewModel) pattern and how it works in Silverlight.  We’ll look at some data-binding with Silverlight controls and how easy it is to manage your application with a good pattern.

Hope to see you there!

On March 26th, I'll be doing a presentation on Silverlight Animation at Edmug.  I'm going to touch on some of the basics for Silverlight Animation, but more focused on a coder's perspective; coding storyboards, event driving animations etc.  If you're in the area, feel free to drop in and heckle me from the back of the room, if there's anything you'd like to see in my presentation leave a comment below.

Also Jean-Paul S. Boodhoo is in town on February 26th, presenting on "BDD style testing".  If you haven't seen JP present, then you should make time on the 26th.  If you have seen him present, I know I'll see you on the 26th.

17 December 2008

If you're connecting to services with Silverlight then chances are you'll use either the WebClient or the HTTPWebRequest.  There's a ton of articles out there about implementing both objects but I wanted to cover a few things that I've learned the hard way in the hope that you won't have to.

 

WebRequest VS WebClient

 

So which one do you use?  Well, WebClient is a more abstracted object, with WebRequest under the hood, so it really depends on what you need to do.  For simple uploading of a string, then WebClient is just fine, but if you want to get into changing headers, or uploading binary data then you'll probably need the WebRequest.

 

ClientAccessPolicy.xml

 

As Silverlight is a client-side framework, it can’t just go crossing domains as it feels like, so to allow access for a Silverlight application, you must have this file on the root of your domain.  If you haven't heard of this before then memorize it now!  Click Here to read up on it and get a sample of the default settings for this cross domain config file.

HTTPCodes 

HA!  You got me Microsoft!  You got me good!

There's only two codes in the WebResponse object, StatusCode.OK & StatusCode.NotFound with Silverlight.  "OK" is equivalent to HTTP status 200 and "NotFound" covers the rest ... well isn't that handy! 

But here's the kicker ... if you receive anything other than a code of 200 then you'll be unable to read the response from the response stream. 

I haven't got a firm answer on the behavior here, but I've certainly come across other people that have had this problem.

So if you're setting the response codes on the service to be specific to what's going, with an error message that you need to read out of the body ... then you'll need to rethink your approach.  Otherwise you won't be able to read the response out of the response object.

 

Binary Data

One thing that I've come across that I think is important to note is trying to read binary data out of a stream in Silverlight.  For the most part you'll use "StreamReader" to read the response streams out of the Response object. 

However, with binary data that doesn't work very well.  StreamReader is just meant for reading text, so you'll want to use a "MemoryStream" instead.  The code is the same as when you're reading text out of the response stream with a "StreamReader".

 

QueryString & Special Characters

If you're sending parameters to a service using the QueryString then you might need to think about special characters, especially if you're dealing with user input.  In this case you're going to find that special characters will cause all sorts of problems if you don't encode them first.

 

Here's an example of encoding your querystring parameters, it must be noted that you should only encode the data for the querystring and not the whole querystring itself.

var queryStr = "?id=" + ID + "&UserInput=" + HttpUtility.UrlEncode("stuff &^/{}%$");

---

Living to Code != Coding to Live