SilverLight : Load Assembly on Demand

Step 3. Create a Silverlight web application

Step 4. Add Reference to SampleLibrary.  After adding reference right-click on the SampleLibrary reference and change the property “Copy Local” to false. This will keeping the Assembly out of the XAP.

Step 5. Copy the assembly to ClientBin folder of web application where the .xap file resides.

Step 6. Add following to to load Assembly.

WebClient : Provides common methods for sending data to and receiving data from a resource identified by a URI.

        Dim wc As WebClient = New WebClient()

        AddHandler wc.OpenReadCompleted, AddressOf wc_OpenReadCompleted
wc.OpenReadAsync(New Uri(“SampleLibrary.dll”, UriKind.Relative))

After loading assembly from web server, AssemblyPart class’s Load method loads it into the appdomain.

    Public Sub wc_OpenReadCompleted(ByVal sender As Object, ByVal e As System.Net.OpenReadCompletedEventArgs)

        Dim part As AssemblyPart = New AssemblyPart

        If Not e.Result Is Nothing Then
part.Load(e.Result)

            UseClassLibrary()

        End If

    End Sub

Step 7. To use Strong Typing:   Do not create the on-demand assempbly class object inside wc_OpenReadCompleted as silverlight’s JIT complier will come in the way. When JIT compiler compiles wc_OpenReadCompleted method, it scans the method, finds that it references a type named “SampleClass” so tries to load “SampleLibrary.dll” so that reference can be resolved. Unfortunately, this happens before the method is even executed.
So always use the on-demand assembly classes in another method. In this way you can enjoy the benifits of strong typing

    Public Sub UseClassLibrary()
Dim objSample As SampleLibrary.SampleClass = New SampleLibrary.SampleClass

        Show.Text = objSample.SampleFunction()

    End Sub

150 150 Burnignorance | Where Minds Meet And Sparks Fly!