Just to be clear, this is not a necessary step for us to take in our conversion process. But it’s a step that spreads some light on some of the differences regarding how you write code for Windows Phone and Windows 8. You can even say that it will show how much easier life gets with C#5 and .NET 4.5.
We’ll take a look a the heart of the application – where the data is fetched from the server. Usually, I prefer to do a web request using HttpWebRequest instead of WebClient. Mostly because a callback from a HttpWebRequest is not executed on the UI thread (explained here by Andreas). This gives us the opportunity to do some heavy lifting on a background thread (such as parsing JSON or XML) not ruining the user experience.
But now – the much loved HttpWebRequest, must step aside for HttpClient that came with .NET 4.5.
This post is a part in a series – you can find the full index here.
This code is more or less directly copied from our Windows Phone application. As you can see we do a simple web request.
public void DownloadData(string url, Action<JsonObject> handleResponse, Action<Exception> handleException)
var httpWebRequest = WebRequest.CreateHttp(url);
var requestContext = new RequestContext(handleResponse, handleException, httpWebRequest);
private void RequestCompleted(IAsyncResult result)
var requestContext = (RequestContext)result.AsyncState;
var httpWebRequest = requestContext.Request;
using (var webResponse = httpWebRequest.EndGetResponse(result))