Search code examples
silverlightcachingmef

Understanding Silverlight, MEF, on-demand Xap loading and caching


I'm currently successfully using MEF to load-on-demand Xaps in my Silverlight 4 application. I am now looking into increasing performance through the use of caching. It is my understanding that MEF uses the WebClient to download the Xap, which in turn will use the browser's downloading mechanism, and is therefore subject to its caching policy.

In my testing, the results I'm seeing are slightly confusing, and results differ between browsers. Obviously, on the first access, with my cache cleared, the on-demand Xaps are requested from the server. And, for the duration of an application session, Xaps are downloaded only this once. All good so far.

However, I was expecting (or at least hoping) Xaps to be cached between browser sessions as well. But no - I observe the following (using fiddler):

Internet Explorer

If I refresh the browser (Ctrl+F5) then the on-demand Xaps are not requested from the server, and are loaded from the local cache. But If I restart the browser, then everything is downloaded again. Boo.

Firefox and Chrome

If I refresh the page (Ctrl+F5) then the on-demand Xaps are requested again from the server - no caching occurs at all. Boo. And obviously, no caching occurs if I restart the browser.

The ideal behavior for me is for the browser, when it needs to load a Xap, to query the server with an If-Modified-Since header to see if a new version exists, and if so download it, and if not, load it from its local cache. But in none of my testing did I see an If-Modified-Since header sent to the server. So my question:

Is there any way to achieve this transparently using MEF? Or another framework? Or do I have to roll my own caching layer using isolated storage (yuck)?

Seems like on-demand Xap loading should go hand in hand with caching, so I'm surprised this doesn't just work out of the box.


Solution

  • OK I figured it out just after I posted this question. I thought I'd share the solution here in case anyone else has the problem:

    I was using the built in Visual Studio web server to host my project. It appears that it doesn't support caching at all. But as soon as I hosted my project in IIS, I saw the exact behavior I desired, specifically:

    The ideal behavior for me is for the browser, when it needs to load a Xap, to query the server with an If-Modified-Since header to see if a new version exists, and if so download it, and if not, load it from its local cache

    In Internet Explorer at least, I can now see it sending If-Modified-Since headers, and receiving the 304 Not Modified response for recently accessed Xaps. Perfect!