Silverlight - Application Structure - 1

by rahul 10/12/2010 2:20:15 PM

This post will try to shed some light on the Silverlight Application and cater to the following questions…

1. How is a XAP file formed and what are the contents inside it?
2. What would be a good way to maintain the size of an application - Application Library Caching?
3. How to load assemblies on demand?


    Create a new Project, called ApplicationStructure using IIS. This is required since later on we will need to use Fiddler to analyze request and response packets.

SNAGHTML960979e

    Add a new Project to this solution… called SilverlightApplication, click Ok

SNAGHTML965363d

    You will get another prompt. Click Ok.

SNAGHTML9664338

    This is how the project structure will look like…

image

    Get rid of MainPage.xaml. Delete it.
    Add two new pages called Page1.xaml and Page2.xaml

SNAGHTML9688b8d

    The following screenshot shows the event that ensures your appropriate page loads when you start your Silverlight application. This is like your Main() method. We'll change this MainPage to Page1, or Page2 as required.

SNAGHTML96a677a

    Before you proceed any further, change MainPage to Page1 or Page2, and ensure your Silverlight Application comes up fine.
    Page1.xaml looks as follows..

<navigation:Page x:Class="SilverlightApplication.Page1" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="Page1 Page">
    <Grid x:Name="LayoutRoot">
        <TextBlock x:Name="txtMessage">This is Page 1</TextBlock>
    </Grid>
</navigation:Page>

    Page2.xaml is exactly same as Page 1 except that TextBlock reads… Page 2.
    Okay, the fun part begins now…
    Check your Solution Explorer.

image

    The XAP file that you see here is a ZIP file renamed as XAP.
    Notice the following when you open the SilverlightApplication.ZIP. There is an AppManifest.xaml that contains information about DLLs as <AssemblyPart> tag.

image


    Okay, let's add a couple of libraries and see how all this changes.
    Install Fiddler, if you haven't done so already.
    Add a Silverlight class library called SilverLightClassLibrary1

SNAGHTML97cd2e9

    Add a reference in SilverlightApplication.

SNAGHTML982cf51

    Recompile the solution.
    Go back to the ClientBin folder and rename the XAP file to Zip. Notice the presence of SilverlightClassLibrary1.dll.

image


    The way this Project is structured, you will find all the Class Libraries going into the same XAP file.
    This can lead to two major issues…
       1. Bloating up of your XAP file that might take a long time for the end users to download
       2. If the application changes in due course, and the SilverlightClassLibrary1.dll doesn't… STILL THE WHOLE XAP will be downloaded. That's a waste of precious bandwidth.

    Let's see what can be done to make the experience a bit better. What you are going to do next is what we call Application Library Caching.
    Go to the properties of SilverlightClassLibrary1. Sign this assembly with a new Key as you can see below.

image

    Recompile the class library and go to the Debug folder. Create a new file called SilverlightClassLibrary1.extmap.xml

image

    Modify the XML as follows… Don't forget to change the Public key token, or it won't work the way it is expected to!

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

    One last thing, and you will be all set from configuration perspective. Change the Silverlight application property to
Reduce XAP size.

image

    Done. Now recompile and you will notice that that the folder structure for your web application would change to…

image

   Notice that there is a new Zip file created for you.
   Also notice the AppManifest.xaml inside the xap file. You will find Deployment.ExternalParts in addition to  Deployment.Parts!

image


   Time to test the application now. Modify SilverlightClassLibary1 -> Class1.cs

namespace SilverlightClassLibrary1
{
    public class Class1
    {
        static public string CurrentTime()
        {
            return DateTime.Now.ToString();
        }
    }
}

    In the Page.xaml.cs… do the following modification…

public Page1()
{
    InitializeComponent();
    txtMessage.Text = "Time now is " + SilverlightClassLibrary1.Class1.CurrentTime();
}

    Run Fiddler, and browse /ApplicationStructure/SilverlightApplicationTestpage.aspx">http://<YOUR_HOSTNAME>/ApplicationStructure/SilverlightApplicationTestpage.aspx

image
    Notice the status code is 200.
    Open a new IE Window and browse your application again…

image


    Notice that instead of Status code 200, you will now get 304… which means "Not Modified". Also notice the body size is 0 KB.
    In the next post, we will build on top of this application. You will find out how you can load the assembly later when required, instead of everything being available at one shot when you browse your application. Stay tuned!

Hope this helps, Wave
Rahul


Quote of the day:
Too many people are thinking of security instead of opportunity. They seem more afraid of life than death. - James F. Byrnes


blog comments powered by Disqus

Rahul Soni

Rahul Soni  Twitter

 LinkedIn

 Facebook

 Email me



Vivek Kumbhar

Vivek Kumbhar  Twitter

 LinkedIn

 Facebook

 Email me


Stack Exchange

profile for Vivek at Server Fault, Q&A for system administrators and IT professionals

profile for Rahul Soni at Stack Overflow, Q&A for professional and enthusiast programmers

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

All Items
Sign in

Visit Microsoft's Site

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2012, Rahul Soni

Powered by BlogEngine.NET 1.4.5.0