post icon

Silverlight Tip of the Day #14 – Dynamically Loading a Control from a DLL on a Server

In this tip I will be showing you how to load a Silverlight control from a DLL that is on a server somewhere. This technique is useful if you want the initial XAP download size of your application kept to a minimum. That is, the controls and resources you place in the DLL are items that you can download on demand when your application actually needs to load them.

To start, I will create a simple control that displays an image. This control will be packaged in a Silverlight class library DLL.

1. Open Visual Studio.

2. Create a new Silverlight application

3. Right click on the top node in your Solution Explorer. Choose  Add->New Project.

4. Select “Silverlight Class Library” and give it a name (mine is called TestCtrl). This project will create the DLL that will contain your control(s).

5. In your class library add a new a new Silverlight User Control (mine is called ImageTest).

6. Open up the XAML page (ImageTest.xaml) for this user control and add something like an Image control. If you are using a relative path to an image, make certain the image you reference is also included in your class library project.

<Image Source="scarlett.jpg"></Image> 

At this point your class library DLL is good to go. Build the and place it on your server in a location you can reference. Note that after building the DLL you will find it in your projects Bin\Debug folder. Clicking the “Show all Files” in the toolbar for your Solution Explorer will make this bin folder visible.

image

7. Next, proceed to your Silverlight Application opening up MainPage.xaml.cs

8. Add a reference to System.Reflection;

9. Use the WebClient class to download the DLL from the server. From the purpose of this demo, I have placed my test DLL at http://www.flaired.com/TestCtrl.dll 

private Assembly _testCtrl; 
 
public MainPage() 
{ 
    InitializeComponent(); 
 
    WebClient downloader = new WebClient(); 
    string path = "http://www.flaired.com/TestCtrl.dll"; 
    downloader.OpenReadCompleted += new OpenReadCompletedEventHandler 
      (downloader_OpenReadCompleted);  
    downloader.OpenReadAsync(new Uri(path, UriKind.Absolute)); 
} 
 
void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
{ 
    AssemblyPart assemblyPart = new AssemblyPart(); 
    _testCtrl = assemblyPart.Load(e.Result); 
    UserControl control = (UserControl) 
    _testCtrl.CreateInstance("TestCtrl.ImageTest"); 
    LayoutRoot.Children.Add(control); 
}

10. Before you run this code, as a final step, make certain to have a ClientAccessPolicy.xml file located in the same location as your DLL. Silverlight will look for this file for permissions when making calls cross-domain. By default Silverlight only allows site-of -origin communication.

<?xml version="1.0" encoding="utf-8"?> 
<access-policy> 
  <cross-domain-access> 
    <policy> 
      <allow-from http-request-headers= "*"> 
        <domain uri="*"/> 
      </allow-from> 
      <grant-to> 
        <resource path="/" include-subpaths="true"/> 
      </grant-to> 
    </policy> 
  </cross-domain-access> 
</access-policy>

Demo:

[silverlight: Tip14_DLLLoading.xap]

Thank you,

–Mike

6 Comments

Leave a comment
  1. Kevin Daly
    06. May, 2010 at 10:10 am #

    Great tip – and I’d just like to say that’s the most awesomely awesome and full of awesomeness demo I’ve seen in a long time :-)

  2. ozz
    06. May, 2010 at 2:28 pm #

    Mike is that you pic??

    Seriously nice tip!

  3. Snowman
    06. May, 2010 at 3:27 pm #

    If only… :)

  4. Laith
    06. May, 2010 at 5:13 pm #

    I love this blog! Keep ‘em coming, Mike.

  5. Albrecht
    07. May, 2010 at 12:49 pm #

    Amazing tutorial. I was wondering if we can use this solution to protect our silverlight’s application from stealing? I mean if we locate an important part of our app on server and only small part goes to user. This small part would have obfuscated fields and our string path would look like 0x22063e3c instead flaired.com/TestCtrl.dll And someone who wants to steal our app won’t be able to find it or Am I wrong? Please answer what do you think about it :)

  6. Snowman
    07. May, 2010 at 6:19 pm #

    If you disble directory browsing in IIS it would make it a lot harder to discover. I’m no expert in security though.