How to Prepare Your Class Library to Support Application Library Caching?


Few days ago, I published one article on "Application Library Caching in Silverlight 4". In that article, we discussed about Library Caching concept in detail with a small demo. We discussed various parts of the AppManifest to support this feature for 3rd party libraries. But sometime you may need this feature for your own custom libraries too to reduce the XAP file size by separating the external assemblies in their own ZIP file, which will download by the app when it is require.

 

Let us discuss on this topic today by a small example which will help you to understand how to prepare your dll library to support Library Caching by your application. Read more to learn about it.

 

What is Application Library Caching?

First of all, let us discuss on "Application Library Caching". Assembly caching is not a new thing in Silverlight 4. It was present since Silverlight 3. Already 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 the WebClient.

 

And this Application Library caching does the similar thing for you very. Suppose, if you have a bigger application and used a huge external libraries, either 3rd part or your own, this easy step will help you make a separate zip file for them which can be downloaded on demand without writing any additional code.

 

Read more about the feature in this blog post: Application Library Caching in Silverlight 4.

 

Prepare Project

Before starting let us create our own Silverlight application solution, where we will have a Class library project too. It is not require to put the class library project in the same solution but for the demo purpose, we will use the same solution here.

 

Below is our project structure where we have our own custom library called "ExtensionLibrary", one Silverlight application named "LibraryCachingDemo" and the hosting web project called "LibraryCachingDemo.Web":

 

Library Caching Demo - Project Structure

 

Once we are done with the project creation, we will build the project which will generate the dll file for the class library project. Just add the Assembly Reference of this DLL to the main application project and build it once again. This will create the application XAP with the assembly added inside that.

 

Library Caching Demo - Add Assembly Reference of Class Library

 

Now it's the time to split out the library out of the XAP and use the feature of Application Library Caching.

 

Signing the Class Library

Before implementing the same in our application, it is require to sign the assembly of the library. To do this, right click on the library project and click Properties from the context menu. This will open up the properties page. As shown below, go to the "Signing" tab and create a Strong name key file and sign the assembly with that:

 

Library Caching Demo - Signing the Library

 

After signing the assembly, build the project to recreate the assembly which is signed with a strong key. Now you need to extract the "Public Key Token" out of the dll. To do this, the easiest process is installing the dll in the GAC by giving the command "gacutil -i <DLL_FILE_PATH>" from the Visual Studio Command Prompt. This will register it in GAC. Make sure to open the Visual Studio Command Prompt as an Administrator.

 

Here is the screenshot of the same:

 

Library Caching Demo - Add Library to the GAC

 

After this step, go to the GAC and extract the public key token from the installed assembly. Note down the key as it will be require in the next step. After you get the key, you can remove the assembly from the GAC as it is not required in next steps.

 

Generate the Manifest

Once you have a signed copy of the assembly with a Public Key Token, go to the original dll file path (in our case, the Bin\Debug folder) and create a new file called "ExtensionLibrary.extmap.xml". Make sure that, this file has the same name of the dll assembly.

 

Library Caching Demo - Create Assembly Manifest File

 

 

Now open the said file and add the following XML code into that:

 

Library Caching Demo - Create Assembly Manifest Definition

 

 

This actually adds the manifest for the dll. Make sure that, you entered the name of the assembly, version, public key token, relative path and the name of the ZIP file properly as shown above.

 

Here is the complete XML code for your reference:

 
<?xml version="1.0"?>
<manifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <assembly>
    <name>ExtensionLibrary</name>
    <version>1.0.0.0</version>
    <publickeytoken>38955262d53ca29f</publickeytoken>
    <relpath>ExtensionLibrary.dll</relpath>
    <extension downloadUri="ExtensionLibrary.zip" />
  </assembly>
</manifest>

 

This step will ask the compiler to not include the dll inside the XAP and create a separate ZIP file for the said assembly if application library caching is set for the application.

 

Adding Support for Application Library Caching

This is the final step. Right click on the Silverlight Application project and go to it's properties panel. In the "Silverlight" tab, just check the "Reduce XAP size by using application library caching" option as shown below:

 

Library Caching Demo - Enable Application Library Caching

 

 

Now build your application once again and this time you will notice that one ZIP file named "ExtensionLibrary.zip" has been created in the "ClientBin" folder of the application project. Also open the XAP file and this time you will notice that the dll file is not present in the XAP.

 

Library Caching Demo - Assembly Output as External ZIP

 

Download

You can download the complete Source Code of the article from here:

 

End Note

Hope this article was helpful to you to understand it properly. As I said earlier, don't forget to read the main article "Application Library Caching in Silverlight 4" because that will give you much broader visibility to the concept.

 

If you liked this, don't forget to leave your feedback at the end of the page. Also, don't forget to share it with others. This will help them to learn about it.

 

By the time when I was preparing the article, Unni posted one link to my previous article as comment, where he demonstrated it in depth. I read that and it was also a good demonstration of the library caching. Don't forget to read it out here: "XAP Optimization – Part II Versioning".


5 comments

  1. Thanks for the post Kunal as always :-)

    ReplyDelete
  2. Love the concept flow with arrows and good technical details, enjoyed it. Thanks,

    ReplyDelete
  3. Why OOB applications are not supported?

    ReplyDelete
  4. We can use custom libraries xap ,this application library support to manifest ,nice article sharing.blog hosting review

    ReplyDelete
  5. Hello... following your article i have created 2 sample projects same as what you explained here one with application library caching enabled and another one without application library caching.. when i published both of them on server and checked the performance of the same using firebug i found that the one in which application library caching is used took more time to load.. I understand first time it takes more time to load but then at later stage once it is in cache it should load faster as compared to other one but this is not happening . can you please help me for that ?

    ReplyDelete


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