<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Silverlight &#124; WPF &#124; Microsoft.Net &#187; reflection</title>
	<atom:link href="http://joel.neubeck.net/tag/reflection/feed/" rel="self" type="application/rss+xml" />
	<link>http://joel.neubeck.net</link>
	<description>Simplifing structure without changing results</description>
	<lastBuildDate>Fri, 01 Apr 2011 21:34:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Silverlight how to: On-demand assembly deployment</title>
		<link>http://joel.neubeck.net/2008/03/silverlight-how-to-on-demand-assembly-deployment/</link>
		<comments>http://joel.neubeck.net/2008/03/silverlight-how-to-on-demand-assembly-deployment/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 17:44:42 +0000</pubDate>
		<dc:creator>joel</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[AssemblyParts]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[microsoft.net]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[StreamResourceInfo]]></category>
		<category><![CDATA[WebClient]]></category>
		<category><![CDATA[XAP]]></category>

		<guid isPermaLink="false">http://joel.neubeck.net/2008/03/silverlight-how-to-on-demand-assembly-deployment/</guid>
		<description><![CDATA[One of the cool new features of Silverlight 2 is the concept of on-demand deployment of assemblies. In most interactive development there is a desire to minimize the amount of data a user has to download to get your RIA up and running. The application package (XAP) size can grow quite quickly if one tries [...]]]></description>
			<content:encoded><![CDATA[<p>One of the cool new features of Silverlight 2 is the concept of on-demand deployment of assemblies.  In most interactive development there is a desire to minimize the amount of data a user has to download to get your RIA up and running.  The application package (XAP) size can grow quite quickly if one tries to include all of resources required to run the entire application.  In Silverlight 2 we have the concept of a XAP file or compressed package (zip file).  By default, the XAP contains an application assembly and any Silverlight assemblies that your application is dependent on (System.Xml.Linq, etc).  In Silverlight 2 we have the ability to generate more than one XAP file giving us the opportunity to deploy additional assemblies on demand, asynchronously to the initial payload.</p>
<p>To leverage this technology one has to understand the concept of “AssemblyParts” and how reflection is used to extract an assembly from a downloaded XAP.  The following example will show how one can code this functionality in Silverlight 2 Beta 1.</p>
<p>The first step in downloading an additional assembly is to go and get the XAP package.  Note the use of a BaseAddress.  In my example the base address is http://localhost/MultipleXap_Web/.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="c-sharp" style="font-family:monospace;">Uri addressUri = new Uri(_baseUri, &quot;ClientBin/MultipleXap1.xap&quot;);
System.Net.WebClient wcXap1 = new System.Net.WebClient();
wcXap1.BaseAddress = _baseUri;
wcXap1.OpenReadCompleted += new OpenReadCompletedEventHandler(wcXap1_OpenReadCompleted);
wcXap1.OpenReadAsync(addressUri);</pre></td></tr></table></div>

<p>Once downloaded, the “OpenReadCompleted” event will fire and one can extract the assembly from the XAP.  I have chosen to factor into a method the logic used extract the Assembly from the compressed zip file.  In the event that we had multiple assemblies we could use Linq to navigate the xml of AppManifest and programmatically load each assembly.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="c-sharp" style="font-family:monospace;">public Assembly LoadAssemblyFromXap(string relativeUriString, Stream xapPackageStream)
{
    Uri uri = new Uri(relativeUriString, UriKind.Relative);
    StreamResourceInfo xapPackageSri = new StreamResourceInfo(xapPackageStream, null);
    StreamResourceInfo assemblySri = Application.GetResourceStream(xapPackageSri, uri);
&nbsp;
    AssemblyPart assemblyPart = new AssemblyPart();
    Assembly a = assemblyPart.Load(assemblySri.Stream);
    return a;
}</pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="c-sharp" style="font-family:monospace;">void wcXap1_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    if ((e.Error == null) &amp;&amp; (e.Cancelled == false))
    {
        // Convert the downloaded stream into an assembly
        Assembly a = LoadAssemblyFromXap(&quot;MultipleXap1.dll&quot;, e.Result);
&nbsp;
        /****************************************************************
         * The following should work  since I did the following
         *
         *   &quot;added a reference to second assembly then specified that 
         *    the library assembly that is referenced by the application
         *    project is not included with the application package
         *    when the application assembly is built&quot;
         *   
         *  This syntax compiles, but DOES NOT run correctly
         ****************************************************************
         * MultipleXap1.Page page = new MultipleXap1.Page();
         ********************************************************/
&nbsp;
        object page = a.CreateInstance(&quot;MultipleXap1.Page&quot;);
        //Insert UserControl into the Primary grid.
        this.LayoutRoot.Children.Add(page as UserControl);
    }
}</pre></td></tr></table></div>

<p>Once the assembly has been extracted, we can create an instance of the selected UserControl by using  the &#8220;CreateInstance&#8221; method on the Assembly object.  Based on Microsoft&#8217;s documentation, we should have been able to add a project reference to the on-demand XAP, within our default Silverlight Application.  This would give us access to that second assemblies types.  We can make the reference and the project compiles, but throws an exception at runtime. CreateInstance is not a preferred approach since it drastically limits how we can leverage the strong typing of objects instantiated from this reflected assembly.</p>
<p>Code: <a href="/wp-content/uploads/2008/03/MultipleXap.zip" onClick="javascript: pageTracker._trackPageview('/code/MultipleXap.zip'); ">MultipleXap.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joel.neubeck.net/2008/03/silverlight-how-to-on-demand-assembly-deployment/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

