Sonntag, 15. Mai 2011

Windows Phone 7–the importance of ? for web service calls

In an older blog post I have mentioned that Windows Phone uses some magic caching mechanism if you call a web service with the same Uri. Because I have the problem now again with a REST-service in my new demo app, I will explain a possible workaround for this caching behaviour a little more in detail.

The problem

For example the call of the RESTfull service TopList 

http://localhost:57988/BornToMoveService.svc/TopList
will not invoked by your Windows Phone 7 client if you call this service a second time. The Windows Phone OS caches the service calls and will give you back the same results as after your first call. For example the event wc.DownloadStringCompleted of the WebClient class the same results as after your first call.
If you now will have the idea to destroy your instance of your WebRequest or WebClient class to force a non-caching behaviour – dont’t waste your time. The caching behaviour is still there.

The solution

The only way to get rid of this caching behaviour is to add a unique sign at your Uri. In this sample I use a Guid and put it after the normal REST-Url.

 
public void LoadData()
{
    ReadJsonDataAsync<TopListEntry>(BaseUrl + "TopList", list => this.TopList = list);
}
 
private static void ReadJsonDataAsync<T>(string url, Action<ObservableCollection<T>> resultAction)
{
    var wc = new WebClient();
    wc.DownloadStringCompleted += (s, e) => OnReadJsonDataCompleted(e.Result, resultAction);
 
    wc.DownloadStringAsync(new Uri(url+ "?id="+ Guid.NewGuid().ToString()));
}

Keine Kommentare:

Kommentar veröffentlichen