Click here to Skip to main content
Click here to Skip to main content

Steps to Discuss Application Library Caching in Silverlight 4

By , 14 Mar 2011
 

Introduction

Sometimes, your Silverlight application becomes too large and creates a bigger XAP file. This causes an 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 sometimes include 3rd party assembly references. In such a case, storing them in a separate XAP and writing the code for downloading them on demand will be very 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 if sharing the same with in-depth details.

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 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 are 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 number 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, all the XAP files are nothing but a ZIP file, hence you can easily convert it to a Zip file by renaming its 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 tell 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 that 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 for the Application Manifest. Go to the output directory "ClientBin". You will see a single XAP file with a number 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 required 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.

History

  • 14th March, 2011: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

_ Kunal Chowdhury _
Software Developer
India India
Member
Kunal Chowdhury is a Microsoft MVP (Most Valuable Professional) in Silverlight Technology, a Codeproject MVP & Mentor, DZone MVB (Most Valuable Blogger), Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working as a Software Engineer II in an MNC located at Pune, India. He has a very good skill over XAML, C#, Silverlight and WPF. He has a good working experience in Windows 7 application (including Multi-touch) development too.
 
He posts his findings in his technical blog. He also writes for SilverlightShow and Codeproject portal. Many of his articles were highlighted as "Article of the Day" in Microsoft sites.
 
He also has another website called Silverlight-Zone.com where he posts article links on Silverlight, Windows Phone 7 and XNA accumulated from various web sites to help the community grow on specified technologies.
 
You can reach him in his Blog : http://www.kunal-chowdhury.com
He is also available in Twitter : http://twitter.com/kunal2383

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionfacing issue with the Application Library Cachingmemberanildcsc7 Dec '11 - 2:38 
Hi Kunal,
Thanks for the article on Application Library Caching in Silverlight 4.I have tried to implement that in my application.However I'm facing an issue while the application gets loaded.The error says "failed to download the platform extension: System.ComponentModel.DataAnotations.Zip".Could you please suggest if something needs to be done to resolve this.
 
Also , I have identified that some of the third party dll's did not get cached and they still exist in the XAP. Is there any specific procedure where we can add the few dll's for caching and exclude few.
 
Thanks ,
Anil Kumar Devarapalem.
AnswerRe: facing issue with the Application Library Cachingmvp_ Kunal Chowdhury _7 Dec '11 - 17:00 
Hello Anil,
 
Please follow these steps documented here: http://www.kunal-chowdhury.com/2011/08/how-to-prepare-your-class-library-to.html[^] and let me know, if you need further assistance. Stay tuned to my blog for various articles, news and tutorials on Silverlight, Windows Phone 7 and Windows 8 Metro UI.
Regards - Kunal Chowdhury
Microsoft MVP (Silverlight) | Codeproject MVP & Mentor | Telerik MVP

 
Follow me on: My Technical Blog | Silverlight-Zone | Twitter | Facebook | Google+

GeneralMy vote of 5memberjugalpanchal1 Jul '11 - 3:28 
Nice.
GeneralRe: My vote of 5mvpKunal_Chowdhury31 Jul '11 - 7:42 
Thanks Jugal.
Silverlight 5 Tutorials :   1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

GeneralMy vote of 5memberAnil_Saran27 Mar '11 - 17:55 
Great and cool one Smile | :)
GeneralRe: My vote of 5mvpKunal_Chowdhury18 Apr '11 - 5:08 
Thank you Sir... Smile | :)

Regards - Kunal Chowdhury | Microsoft MVP (Silverlight) | CodeProject MVP | Software Engineer
 
My Latest Blog Feed [04-April-2011]:  A Complete Guide to Expression Blend 4 Shortcut Keys   [^Blog^]

Appreciate your vote and feedback

GeneralMy vote of 5memberE$w@r23 Mar '11 - 8:24 
Good work man. Keep up
GeneralRe: My vote of 5mvpKunal_Chowdhury23 Mar '11 - 16:19 
Thank you E$w@r for the support.

Regards - Kunal Chowdhury | Microsoft MVP (Silverlight) | CodeProject MVP | Software Engineer
 

GeneralMy Vote of 5memberRaviRanjankr19 Mar '11 - 20:00 
WOW.. Nice Article! thanks for share it.
GeneralRe: My Vote of 5mvpKunal_Chowdhury19 Mar '11 - 20:09 
Thanks Ravi for the support.

Regards - Kunal Chowdhury | Microsoft MVP (Silverlight) | CodeProject MVP | Software Engineer
 

QuestionWhat do you mean bymvpSacha Barber17 Mar '11 - 7:08 
Rest of the dll ZIP will be downloaded on demand whenever require by your application.
 
Could you please let me know what you mean by that in the context of this article, so what when I used a resource of something in one of those resources in my Xap, the theme Zip will be downloaded?
Sacha Barber
  • Microsoft Visual C# MVP 2008-2011
  • Codeproject MVP 2008-2011
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
 
My Blog : sachabarber.net

AnswerRe: What do you mean bymvpKunal_Chowdhury17 Mar '11 - 22:23 
Hi Sacha,
 
Suppose, you created some resource dlls and included them in your main project. Now once you enable the library caching, it will create separate ZIP files for those dlls. The ZIP will be downloaded to client PC when it hits for the first time.
Sacha Barber wrote:
so what when I used a resource of something in one of those resources in my Xap, the theme Zip will be downloaded?

Exactly. If application library caching is enabled, it will create separate ZIP files for those external assemblies at the time of build and will be downloaded on first use.
 
Let me know, if you have any more concerns. One more this: it will not work for OOB applications. Appreciate your support.

GeneralRe: What do you mean bymvpSacha Barber17 Mar '11 - 22:34 
Cool
Sacha Barber
  • Microsoft Visual C# MVP 2008-2011
  • Codeproject MVP 2008-2011
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
 
My Blog : sachabarber.net

GeneralMy vote of 5memberJF201516 Mar '11 - 20:01 
Awesome article. Thanks for sharing!
GeneralRe: My vote of 5mvpKunal_Chowdhury17 Mar '11 - 5:18 
Thank you.

GeneralImagesmvpJohn Simmons / outlaw programmer16 Mar '11 - 8:01 
All of them appear to be missing.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

GeneralRe: ImagesmvpKunal_Chowdhury16 Mar '11 - 8:25 
May be your server blocked those images. I will upload them in CodeProject soon and update this article with them.

GeneralExcellent!mvpNishant Sivakumar16 Mar '11 - 3:11 
Keep up the good work! Thumbs Up | :thumbsup:

GeneralRe: Excellent!mvpKunal_Chowdhury16 Mar '11 - 6:14 
Thanks once again Nish for the support.

GeneralMy vote of 5mvpNishant Sivakumar16 Mar '11 - 3:11 
Good quality stuff as usual, Kunal!
GeneralRe: My vote of 5mvpKunal_Chowdhury16 Mar '11 - 6:13 
Thanks Nish. Thanks for the vote.
 
Today is a very bad day for me. Unfortunately some of my good articles got deleted because they were on 3rd party controls. Someone raised this issue because his article got deleted and hence he raised the same for my articles too. Unfortunately, the hammers took the decision to remove mine too. D'Oh! | :doh: What a bad day for me.

GeneralRe: My vote of 5mvpNishant Sivakumar16 Mar '11 - 6:25 
Kunal_Chowdhury wrote:
Unfortunately some of my good articles got deleted because they were on 3rd
party controls. Someone raised this issue because his article got deleted and
hence he raised the same for my articles too. Unfortunately, the hammers took
the decision to remove mine too.

Yes, that has always been a site policy. No 3rd party articles except in the paid showcase section.

GeneralRe: My vote of 5mvpKunal_Chowdhury16 Mar '11 - 6:38 
Yea... unfortunately... Frown | :(
GeneralRe: My vote of 5mvpNishant Sivakumar16 Mar '11 - 6:41 
Well I don't think there's a safe way to allow 3rd party articles because then there'd be a flood of ads disguised as articles. So unfortunately it's not a very viable option in my opinion.

GeneralRe: My vote of 5mvpKunal_Chowdhury16 Mar '11 - 6:42 
Yes, I can understand that.

GeneralRe: My vote of 5mvpNishant Sivakumar16 Mar '11 - 6:43 
How many articles did you lose due to this?

GeneralRe: My vote of 5mvpKunal_Chowdhury16 Mar '11 - 6:58 
Total 4 articles I lost today, which made today as a black day for me (A Wednesday -> Black Wednesday). Poke tongue | ;-P

GeneralRe: My vote of 5mvpNishant Sivakumar16 Mar '11 - 6:59 
4? Sorry to hear that, Kunal! I am sure you'll write many many more. Thumbs Up | :thumbsup:

GeneralRe: My vote of 5mvpKunal_Chowdhury16 Mar '11 - 7:18 
Yea, feeling very bad as all those were removed on same day because of some issues. Hope to recover this pain early. Big Grin | :-D

GeneralMy vote of 5memberSabarinathan Arthanari15 Mar '11 - 3:22 
My vote of 5 for a good tip
Sabarinathan Arthanari
As a child of God (Truth/Love), I am greater than anything that can happen to me -Dr APJ Abdul Kalam.

GeneralRe: My vote of 5mvpKunal_Chowdhury15 Mar '11 - 6:00 
Thanks Sabarinathan for the vote and feedback.

GeneralMy vote of 5memberPumbaPumba14 Mar '11 - 23:12 
Hi Kunal,
 
My vote of 5. Great tip.
GeneralRe: My vote of 5mvpKunal_Chowdhury15 Mar '11 - 5:59 
Thanks Pumba for the vote. Great to hear that you liked it.

GeneralMy vote of 5mvpBrij14 Mar '11 - 19:46 
Nice one again!!
GeneralRe: My vote of 5mvpKunal_Chowdhury15 Mar '11 - 5:56 
Thanks Brij.

GeneralMy vote of 5membermbcrump14 Mar '11 - 8:43 
Thanks! Great Article.
GeneralRe: My vote of 5mvpKunal_Chowdhury14 Mar '11 - 8:52 
Thanks Michael.

GeneralMy vote of 5mvpAbhijit Jana14 Mar '11 - 8:13 
Nice share Kunal ! You didn't talked anything about OOB based application. It will be great if you add that as well !
GeneralRe: My vote of 5mvpKunal_Chowdhury14 Mar '11 - 8:37 
Thanks for the vote and feedback AJ.
 
Abhijit Jana wrote:
You didn't talked anything about OOB based application. It will be great if you add that as well !

Yea, I forgot to mention that one. In OOB apps, it will not work. I will update it soon. +5ed you for the query.

GeneralMy vote of 5memberSChristmas14 Mar '11 - 7:11 
Good work KC. Keep writing
GeneralRe: My vote of 5mvpKunal_Chowdhury14 Mar '11 - 8:35 
Thanks buddy.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 14 Mar 2011
Article Copyright 2011 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid