Click here to Skip to main content
6,306,412 members and growing! (17,587 online)
Email Password   helpLost your password?
Web Development » Silverlight » HowTo     Intermediate License: The Code Project Open License (CPOL)

Silverlight on Fly loading (RC0)

By Tecfield

This article is about how to manage xap packages at runtime
C# (C# 1.0, C# 2.0, C# 3.0), ASP.NET, XAML, LINQ, Silverlight, Dev, Design
Posted:10 Aug 2008
Updated:30 Sep 2008
Views:10,094
Bookmarked:16 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
8 votes for this article.
Popularity: 3.07 Rating: 3.40 out of 5

1
2 votes, 25.0%
2
1 vote, 12.5%
3
1 vote, 12.5%
4
4 votes, 50.0%
5
DynamicLoading

Introduction

This article is about how to breakdown xap packages at runtime and manage to load them in the most beneficial way ignoring repetitious requests.

In case you are using one Silverlight application for your website, you may be interested in this article. The whole idea behind my code is if I have a huge SL app and all users won't go to all areas and/or not allowed to go some places, it is better to load SL on demand into client machine instead of do it once and in a HUGE XAP package.

Using the Code

I tried to keep usage as simple as possible. There is one key function plus some events and settings need to be done in order to use this code. If you wanted to find out how the system works, you may go through the code. There are some brief comments in code to help you understand it easier.

The followings are libraries need to be referenced:

  1. Tecfield.Silverlight.XapManager.dll (needs to be added to web project)
  2. Tecfield.Silverlight.AssemblyManager.dll (needs to be added to main silverlight project)

There are two StringConnections need to be set in Web.Config file

<add name="SilverlightClientBinFolderPath" connectionString="~/ClientBin"/>
<add name="XapInformationManagerMainPagePath" connectionString="AppLoader"/>

The first line defines the relative path of Silverlight's client bin folder on server.
The second line defines the main silverlight application's name. Keep in mind that the main SL app is the main host to load the rest of apps.

In server-side there is an http handler which all requests from silverlight controls come to it.
This HttpHandler just pass all requests to the XapManager:

public const string AppParameter = "ApplicationName";
public const string LibParameter = "LibraryName";
public void ProcessRequest(HttpContext context)
{
    string appName = context.Request.Params[AppParameter];
    string libName = context.Request.Params[LibParameter];
    context.Response.ContentType = "application/octet-stream";
    string fileName = XapInformationManager.Current[appName, libName];
    context.Response.WriteFile(fileName);
    context.Response.Flush();

    XapInformationManager.DeleteFileIfRequired(fileName);
}

In client-side, there are two objects you need to use them to get proper results.

  1. Request: Each time when you need to load a window, you need to create an instance of this object.
  2. AssemblyManager: This object is the one who manage loading process. You need to create an instance of this object too, but probably one global could be enough.
AssemblyManager asmManager = new AssemblyManager(App.Current.Host.Source.AbsolutePath);

AssemblyManager's constructor needs an initial strings which is HttpHandler's address to communicate with and send requests to. Once you create a new instance of AssemblyManager, it loads currently loaded libraries into its managed list.

Next step is asking for a page to be rendered. The following is the sample code:

Request request = new Request(applicationName, childPath);
request.OnSucceed += new Request.RequestResponse(OnSucceed);
request.OnFailure +=new Request.RequestResponse(OnFailure);

asmManager.LoadApplication(request);

You will be notified through Request events as soon as request loading finished

  • OnSucceed: Fires when the request loaded successfully.
  • OnFailure: Fires when the request failed to be completed. Request.Error will contain the reason.

applicationName refers to Silverlight Application Name i.e. MyControl
childPath refers to the page path within the application to load i.e. MyControl.Page

ATTENTION!

  • DO NOT SPECIFY ANY EXTENSION. All libraries using in this article working with names but no extensions. All extensions are managed by interior code and shouldn't be specified.
  • XapManager just supports SL applications but not libraries. The reason is because of using App.Manifest to load dependencies.
  • If you need to use web-services, instead of normal way use the sample way I put in demo.  

Update Note:

Solution and files are changed to be compatible with Silverlight RC0.
There were two ways to get system to work:

  1. XmlReader: which I have used.
  2. Linq to Xml: This option needs to add another reference to project and makes its size bigger, so, I avoid to use it.

Some More Ideas

I put some more ideas in code for you about how to manage your system and load everything on fly, call on fly, and live on fly :D

I would appreciate any incoming idea or suggestion to improve this system.

License

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

About the Author

Tecfield


Member
I got my BS in Iran, worked there for 4 years mainly in industrial automation field. Then moved to Australia for 2 years working in big companies like Massive and VISEAN (a part of Weatherford US) as a .NET developer and .NET Senior Analyst Programmer. Currently, I have moved to US to get my MS in IS/Internet App Dev in Illinois State University (ISU).
Occupation: Software Developer (Senior)
Location: United States United States

Other popular Silverlight articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralGreat article!!! But the sample does not compile. [modified] Pinmemberreachpoint9:55 19 Aug '08  
GeneralRe: Great article!!! But the sample does not compile. PinmemberTecfield13:41 19 Aug '08  
GeneralRe: Great article!!! But the sample does not compile. PinmemberTecfield2:29 20 Aug '08  
GeneralConfused PinmemberDewey19:47 11 Aug '08  
GeneralRe: Confused PinmemberTecfield13:57 12 Aug '08  
GeneralRe: Confused PinmemberDewey22:21 13 Aug '08  
GeneralRe: Confused PinmemberTecfield14:20 14 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Sep 2008
Editor: Chris Maunder
Copyright 2008 by Tecfield
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project