Silverlight Execution Context Within Browser - silverlight

I need some information on XAP files execution context in the browser. Specifically, how and where the XAP file is loaded when it is loaded into memory.
My understanding is that it would be loaded in the address space of the web browser's process, this would be done once per instance of the browser window.
Reason for this question is I need to understand how static variables (defined in a type located in XAP package) behave and their lifetime,- for example when XAP package is running in multiple browser windows (ie. user opened 3 IE windows but is viewing the same page). This also raises important issue whether static data in static variables can be shared across browsers, which I don't is the case.
What I did notice through my testing is that when I F5 (page refresh) all the static types were re-created (ie. my singletons were re-created, I could see this by seeing all static member fields losing their previous value they had back to default value when class is initialized)
Any links to more reading would be also appreciated.
Thx,
lk

Every instance of Silverlight is sandboxed to that specific instance. Even multiples of the same Silverlight app in the same Browser page are duplicate instances and nothing is shared.
If you refresh the browser you have flushed your program from memory and are starting again.
They are therefore not even shared between browser tabs.
Not much to read about really, just a fact of Silverlight life :)

Related

Can two Silverlight applications share IsolatedStorage on one machine?

What identifies an silverlight application and when can two silverlight applications share IsolatedStorage if at all, i.e.:
if I want to have two Silverlight applications share IsolatedStorage space, is this possible? What kind of "application id" do I need to give to do this?
if I don't want two Silverlight applications to share IsolatedStorage, how do I prevent this? Do I need to do this?
For instance, I've noticed when I develop a Silverlight application, I can press F5, in the application save to Isolated Storage, stop the application, press F5 again, and it reads from the same IsolatedStorage. (I would think that a new compilation would cause it to use new IsolatedStorage.)
However, when I then copy the .xap and .html files to another directory and open the .html file, it does NOT share the IsolatedStorage with the application I was developing. What changed?
What is going on behind the scenes here so I know when IsolatedStorage is shared and when it isn't?
The URL to the source XAP file identifies the application. You would want a new version of an application to be able to read the existing store for a previous version. Consider a game where all the high scores are stored in the application store. The user might be a little upset when all those scores disappear after they upgraded it.
Different applications can share a single site based store. However you only get those two levels of granularity, app level keyed at the XAP Url or site based, which is host and port (I'm not sure whether scheme is also part of that key).
If memory serves me right the isolated storage can be used within the scope of the application and scope of the page. So - if I understand my recollection right, I'd probably say - yes.
Edit
From a copy of Pro Silverlight 3.0 in C# that I posess :
(p. 636)
With isloated storage, a niqe storage location is created for every combination of user and application. In other words, the same computer can have multiple isloated storage locations for the same application, assymin each one is for a different user. Similarly the same user can have multiple storage locations one for each Silverlight Application
(p. 637)
... GetUserStoreForFile(). This method provides a storage site that's accessible to all silverlight applications on the same website domain, however these settings are still user specific

Reduce XAP Size Setting - What's the Benefit?

Using Silverlight 3, I noticed that System.Xml.Linq.dll was added to my XAP file, increasing the size from 12 to 58 k, so I checked the box 'Reduce XAP Size by using application library caching'.
Publishing the app to IIS, then loading it with Web Dev Helper enabled, I see that when I open the app, the XAP file at 12k is loaded, then the System.Xml.Linq.zip is loaded at 46k, for a total of 58k. Whenever I refresh the main page of the app, the same files are loaded into the browser. If I uncheck the 'Reduce..." box, then re-publish the app to IIS, one XAP file at 58k is loaded whenever I load the application.
How is one method different from or better than the other? I could see the advantage if the dll were somehow saved on the client computer removing the need to download it each time the app were opened.
Thanks
Mike Thomas
A browser caches by URL, so by splitting your application into a part which changes frequently and a part which will probably stay the same for a long time (the Linq part) and which might be shared between applications even, you save some download.
But it depends on the exact situation (frequency of change, location of 'generic' DLLs, etc.) whether it really helps.
The whole reason for keeping XAP size small is so that your application loads as quickly as possible. This is important: even on a faster connection, a bloated XAP can take extra seconds to load, which can be long enough for your users to leave your site.
While Linq is only accounting for 46KB, there are other cases where this can make a bigger deal. For instance, the SyndicationFeed class makes it really easy to handle RSS and ATOM feeds, but it weighs in at 114KB.
Application library caching helps in two ways:
It allows for sharing common DLL's between applications, so if another application has already pulled down a system DLL, your app can just reference it.
It allows your application updates to be smaller, since the framework DLL's won't change betwen XAP versions.
The difference is that when dll's are outside of the XAP file even though browser asks for those files webserver responds with 304 Not Modified HTTP response.
By default browser will not request for those files to be downloaded again. This obviously saves time especially when project references "heavy" libraries (ie. Telerik ones can be quite large in size)
Hope this helps someone.

Is Silverlight isolated storage treated as permanent, or as a cache?

How persistent is isolated storage - does Silverlight treat it like a cache, deleting items when it needs more space, or only when the user or application request it?
There also seems to be a wide variety of means to identify isolated storage - machine, application, domain, .... What I'm having trouble with is how these all relate to the user. Is it possible, and if so how, to create and later retrieve an isolated storage file with the following properties:
The same file is used, regardless of which Windows user is logged in
The same file is used, regardless of the assembly version (updates to the xap). Instead the url would remain constant. This would have to work even offline (out of browser).
Basically I want the isolated storage to persist across application updates, and over different users logging in.
It is fairly permanent. The user could delete it if they really wanted too, but they would have to go out of their way to do so.
Here is the MSDN documentation for Isolated Storage.
IsolatedStorageFile in Silverlight a couple of statics that let you choose where you want to scope the storage:
GetUserStoreForApplication
GetUserStoreForSite
The "MachineStore" options are not available in Silverlight, there are just "Application" and "Site". Both are scoped by the user..since the files are stored under the user's AppData on Windows. Apps in-browser and out-of-browser share the same Isolated Storage stores.
[edit..I missed part of your question the first time]: The Isolated Storage stores are not part of the browser cache, so they are not cleared when the browser cache is cleared. As a developer, you can delete things programmatically. As a user, you can use the Silverlight configuration UI (i.e. the right-click menu) to manage the stores - it's called "Application Storage" to the user. Finally, an intrepid user can locate the files on disk and delete them manually...they are hidden so they won't show up in a normal search, but a determined user can still find them.

Question About Silverlight and how the application is loaded

In Silverlight 3.0 we can now create applications with multiple pages using the navigation framework, My question is :
When we run the application does the whole application gets loaded up at once or it gets loaded up when we navigate to a particular page ?
This is probably important for those who want to create an application that gets loaded up very quickly, if everything is loaded up at the begining, the whole user experience might become like a nightmare as they have to wait a long time.
Thank you.
Mmm...
If the entire XAP is loaded check how is stored in browser cache.
On the other hand, you can perform partial loads as markti commented, how to define your views? use interfaces contract, so you can deferr the load the views to show on demand (main XAP loads application, plus interface assemblies, the you divide the views into several DLL's, maybe main page is loaded and the rest just loaded in background).
You can as well use isolated storage to cache that Dll's (then your application will read that Dll's from the local computer).
http://timheuer.com/blog/archive/2008/09/24/silverlight-isolated-storage-caching.aspx
The entire executing XAP is loaded into memory. This is less important because having the Pages defined is not the bulk of the work. The bulk of the work is when the Pages are constructed and then rendered. The Pages are not constructed/rendered until the Page is navigated to.
If your XAP starts to get bulky (causing the initial download to take some time) you can get around that problem by lazy loading XAPs or assemblies using WebClient.

Silverlight MVVM Isolated Storage

I've tried to use IsolatedStorageSettings in my ViewModel, but these are not getting retained across browser refreshes (F5).
for example;
//look in the IsoStore for remembered details
IsRememberMe = IsolatedStorageSettings.ApplicationSettings.Contains(Constants.LOGIN_REMEMBERED_USERNAME);
if (IsRememberMe)
{
UserName = IsolatedStorageSettings.ApplicationSettings[Constants.LOGIN_REMEMBERED_USERNAME] as string;
}
Do I need to do something differently in my MVVM ViewModel's??
EDIT
It's worth noting that this code is sitting in a referenced project - so ultimately a seperate XAP file to the parent XAP that is loaded in the browser - might this cause the settings to be lost on each refresh?
THanks,
Mark
Well...
In my case I have issues using Application Isolated Storage, each time I deployed a new version of my app (just for instance changing the color of a button I lost my Iso Storage :-().
I move to use SiteStorage instead of Application level, and it worked:
http://www.tipsdotnet.com/TechBlog.aspx?PageIndex=0&BLID=13
On the other hand what I had done with Iso Storage is perform CRUD on folders and files, not sure abou that other kind of settings.
HTH
Braulio
I would think one of two things is happening here. Either your binding isn't working correctly in both directions so either the persistence or the retrieval code is never hit. Or, you're storing these values in application level iso storage from two different applications (or something to that effect). Make sure your code is being hit in both cases (storing and retrieving) and make sure you're accessing the iso store from the same place (if you're using application level isolation, store/retrieve from the same application, etc).

Resources