Application Library Caching in Silverlight 4


Sometime your Silverlight application becomes too larger and creates a bigger XAP file. This causes issue while loading your XAP file for the first time as it increases the loading time for downloading the XAp. So, what will you do in this case? There is one answer: split your application in multiple projects which will create multiple XAPs and then use On Demand downloading feature.

 

Absolutely right, but you may sometime include 3rd party assembly references. In such case, storing them in a separate XAP and writing the code for downloading them on demand will be another hectic. So, what's the easy process? In this article we will learn the same. Read to know more and provide your feedback in case you need more information.

 

 

 

 

Assembly caching is not a new thing in Silverlight 4. It was present since Silverlight 3. Today I got a chance to look into it and thought to share the same with depth details to you.

 

So, what is Application library caching? We all know about the on demand download of XAP. To do this, we need to write code for downloading the external xap files using WebClient. Application Library caching does the similar thing for you very easily. Suppose, if you have a bigger application and used a huge 3rd party libraries, this easy step will help you make a separate zip file with them which can be downloaded on demand without writing any additional code.

 

Many people don't know about it and hence let's start describing that here.

 

 

Step 1 - Digging inside XAP:

First of all, we will create a small Silverlight Application project. To do this, open your Visual Studio and create that.

 

image

 

 

Once you done creating the project, in Solution Explorer you will see that it has some default references to some assembly files. Once you build the project, it will create a .XAP file for you. Here is a screenshot of the same:

 

image

 

You can see that, it creates the XAP file in the client bin directory. It creates a single XAP irrespective of no. of assembly files referenced to that project and unnecessarily increases the size of XAP file whether it requires those or not.

 

To see the contents inside the XAP, go to the ClientBin folder. There you will find the XAP output of your Silverlight application.

 

image

 

As you all know that, all the XAP files are nothing but a ZIP file, hence you can easily convert it to a Zip file by renaming it's extension.

 

image

 

 

Open the ZIP file. Inside that, you will find "AppManifest.xaml" (which stores the referenced Assembly information), dll output of your project and all other referenced assemblies. If you add more 3rd party assembly reference in your solution, this list will increase.

 

image

 

 

To know details of that, open the AppManifest.xaml file in a text editor. Have a look into the below screenshot:

 

image

 

 

You will notice that, it has all the referenced dll information as Assembly part, entry point and required runtime version. Assembly parts tells to load the referred assembly directly from the XAP.

 

 

Step 2 - Working with more Assemblies:

Just visualize what we demonstrated in the first step. Now, we will add some additional dll reference in our project. To do that, right click on your project and click "Add Reference'. Now from the add reference dialog, select some 3rd party dlls and include them in your project.

 

For the demonstration, we will add some theming dlls in our solution. To do this, search for "theme" and you will see a list of theming dlls in the add reference dialog. Chose some of them and add into the project.

 

image

 

 

Once you add them, you will notice that the selected assemblies are now part of your project (see the figure below):

 

image

 

 

Now, build your solution and open the new XAP file (rename it to a ZIP). You will see that all the new referenced dlls are now part of your XAP. See the below screenshot, which will give you better information:

 

image

 

 

Let us open the "AppManifest.xaml" and see what it contains now. Wow, it contains all those additional dll entries as AssemblyPart and tells to load them from the XAP itself.

 

 

image

 

This actually increases the XAP size and makes your application little bulky. Thus increases initial loading time of your application.

 

 

Step 3 - Using Application Library Caching:

To overcome this, let us change the setting of our project to use the Application library caching feature now. Go to your project properties panel. In the Silverlight tab, you will find a checkbox called "Reduce XAP size by using application library caching". Check this as shown below:

 

image

 

 

Now build your project once again and you will see, it will create as many .ZIP files as the count of external assemblies as shown below. You will also notice that, it creates a single zip for a single referred dll assembly.

 

image

 

 

Let us go into the deep to lookout the Application Manifest. Go to the output directory "ClientBin". You will see a single XAP file with a no. of ZIP files shown below:

 

image

 

 

Each ZIP contains a single dll file here. Rename and open the XAP file. Open the AppManifest.xaml from the XAP.

 

 

image

 

Here you will notice a single AssemblyPart pointing to your original project output and rest of the dll references are now moved to ExternalParts. They are now pointing to the Zip files instead of the actual dlls.

 

Now, when you run your application, it will only download the XAP file which contains only the main resources. Rest of the dll ZIP will be downloaded on demand whenever require by your application.

 

Hope, this information will help you to understand the library caching feature of Silverlight and give you the chance to use it in your application. Let me know, in case you need further information on that.


9 comments

  1. Very useful information..... thanks kunal.
    i have a question about it.

    As the no. of zip files goes on increasing, the network interaction also increases. So do we have any option to group the related files so that we can optimze network interaction also.

    ReplyDelete
  2. Hello Vishal,

    Automatic grouping is not there, but you can manually group them inside a single ZIP file and modify the manifest file accordingly. This is little tricky. Regarding network interaction, I don't think it will be a problem because, it will download the ZIP only once, when you call it for the first time. As this will load the dlls on first call, you don't have to go to server once again to fetch them.

    ReplyDelete
  3. Thanks kunal for your reply. Your all articles are informative and easy to understand. Thanx for it.

    ReplyDelete
  4. Yes Vishal, the way he represents is very easily understandable for a beginner. I really appreciate his effort for doing such kind of things. Kunal, keep it up.

    ReplyDelete
  5. Hi,
    any idea how to load the same dll with different version number without the createdomain trick? It does not exist in SL.

    txs in advance.

    paul.simons@hotmail.com

    ReplyDelete
  6. @Paul, Yes you can do it but little tricky. http://csharprambling.wordpress.com/2011/06/07/xap-optimization-part-ii-versioning/

    ReplyDelete
  7. @Unni, oops... I was just writing the same topic for tomorrow. Thanks for sharing the link. Will definitely read it once I am done with the said article. Will also give a reference to your article on my post for further reading as you explained it in more details. Thanks again for sharing the link.

    ReplyDelete
  8. Thanks Kunal for sharing very useful information, which explain very nicely with the help of screenshot.

    ReplyDelete
  9. Hi

    I have created a silverlight 4 application. Its working fine in my environment. when i deploy in server & access that application from client, it gets loading for about 40 seconds since xap size is 440KB. On surfing the net, i came to know that by creating gextmap.xml files of third party dll's would created as zip files in client bin of silverlight web project.

    Its for sure that system 3rd party dll's like Theming, etc gets converted. If i use 3rd party dll other than system dll's like coolmenu, etc., it is not converting as zip file. Rather it resides in xap size file itself.

    So how can i make tat coolmenu.dll to be reside in web part of silverlight?

    Thanks in advance.. Any Help., well appreciated..

    Nas..

    ReplyDelete


 
© 2008-2014 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top