Professional, Software

On Demand Download

I have a simple app called “OnDemandApp”. This app uses an assembly “OnDemandLibrary”. This library has set of resources (in this sample that is a WMV file that is about 16 MBs). Application is pretty simple as all it does is pulls out the WMV out of the assembly and plays the video. Though there are other ways to solve the video problem, that seemed to be the easiest way for me to make the assembly big to make sense to use on-demand download.

I created a sample project and only things I have changed are as follows.

Page.xaml

<UserControl x:Class="OnDemandApp.Page"
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <StackPanel x:Name="LayoutRoot" Background="White">
        <Button Name="btndownload" Height="20" Content="Download" Click="Button_Click"/>
    </StackPanel>
</UserControl>

Page.xaml.cs

   1: using System;
   2: using System.Windows;
   3: using System.Windows.Controls;
   4: using System.IO;
   5: using System.Windows.Resources;
   6:  
   7: namespace OnDemandApp
   8: {
   9:     public partial class Page : UserControl
  10:     {
  11:         public Page()
  12:         {
  13:             InitializeComponent();
  14:         }
  15:  
  16:         private void Button_Click(object sender, RoutedEventArgs e)
  17:         {
  18:             PlayMedia();
  19:         }
  20:  
  21:         void PlayMedia()
  22:         {
  23:             Uri mediauri = new Uri("/OnDemandLibrary;component/vihan.wmv", UriKind.Relative);
  24:             StreamResourceInfo streaminfo = Application.GetResourceStream(mediauri);
  25:             MediaElement me = new MediaElement();
  26:             me.SetSource(streaminfo.Stream);
  27:             LayoutRoot.Children.Add(me);
  28:             me.Play();
  29:         }
  30:     }
  31: }

This code is pretty straightforward…

  1. Application.GetResourceStream is the function, I can use to pull out streams for the resources from the assemblies, in this case that is vihan.wmv
  2. I set the source of the media element to be that stream.
  3. I add the media element on the stackpanel
  4. I call play so that video playes

Now to create on demand experience for the big DLLs here is what changed in the Page.xaml.cs

   1: using System;
   2: using System.Windows;
   3: using System.Windows.Controls;
   4: using System.IO;
   5: using System.Windows.Resources;
   6: using System.Net;
   7:  
   8: namespace OnDemandApp
   9: {
  10:     public partial class Page : UserControl
  11:     {
  12:         public Page()
  13:         {
  14:             InitializeComponent();
  15:         }
  16:  
  17:         private void Button_Click(object sender, RoutedEventArgs e)
  18:         {
  19:             //PlayMedia();
  20:             WebClient downloader = new WebClient();
  21:             downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
  22:             downloader.OpenReadAsync(new Uri("OnDemandLibrary.dll", UriKind.Relative));
  23:  
  24:         }
  25:  
  26:         void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
  27:         {
  28:             Stream assemblystream = e.Result;
  29:             AssemblyPart assemblypart = new AssemblyPart();
  30:             assemblypart.Load(assemblystream);
  31:             PlayMedia();
  32:  
  33:         }
  34:  
  35:         void PlayMedia()
  36:         {
  37:             Uri mediauri = new Uri("/OnDemandLibrary;component/vihan.wmv", UriKind.Relative);
  38:             StreamResourceInfo streaminfo = Application.GetResourceStream(mediauri);
  39:             MediaElement me = new MediaElement();
  40:             me.SetSource(streaminfo.Stream);
  41:             LayoutRoot.Children.Add(me);
  42:             me.Play();
  43:         }
  44:  
  45:     }
  46: }
  1. In this case, this assembly is not part of the package but deployed as loose assembly on the webserver next to Xap (app package).
  2. On button Click, I created a WebClient to download the DLL
  3. On OpenReadCompleted Event, I load the stream that webclient returned into and assemblypart.
  4. Use AssemblyPart.Load method to load the assembly from stream.
  5. Now, PlayMedia method can use the video embedded in that assembly

you can also build actual progress UI using DownloadProgressChanged event on webclient.

Advertisements
Standard

7 thoughts on “On Demand Download

  1. Just wondering, will the file be downloaded on demand every time the user uses this program or will it be stored somewhere?

    Looks like it will just remain in the browser’s cache until it’s deleted.

  2. abhijeet says:

    hi i m tryin to do same thing I hav a usercontrol in dat dll…..could u xplain wat xactly u hav done inside

    Uri mediauri = new Uri(“/OnDemandLibrary;component/vihan.wmv”, UriKind.Relative);

    how do i get elements thru dat dll….

    thnx in advance…..

  3. well by stack panels, do you mean you have XAML or actually classes. if you classes, you could just instantiate them. if you have XAML, you could loat the stream , convert it to string and then use XamlReader.Load

  4. lijo says:

    hi vivek nice topic.
    i just want to know.if i have a silverlight class library which contains a file in a folder
    like this

    SlClassLibrary\Dictionary\sl.dic

    sl.dic is a dictionary file.

    how i can access that file in my rest of the application through on demand?.

    actually i tried like this.
    Uri myUri= new Uri(“/SlClassLibrary;dictionary/sl.dic”, UriKind.Relative);
    StreamResourceInfo streaminfo = Application.GetResourceStream(myUri);

    but the stream is getting null value.
    can anybody please help me .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s