Saturday, February 04, 2012
Download

DynamicallyLoadAssemblyies.zip

Purpose

Using WebClient and AssemblyPart we can dynamically load any assemblies. This is greate techniques to use for designing plug and play type of Silverlight application. Also this is greate optimazation technique to make the xap file smaller and lazy load assemblies that might contain business logic.

Imagine that you have silverlight that does not require redeployment and only thing you have to redeploy is your reflectively loaded assemblies during runtime!

DynamicallyLoadAssemblyies.zip

Purpose

Using WebClient and AssemblyPart we can dynamically load any assemblies. This is greate techniques to use for designing plug and play type of Silverlight application. Also this is greate optimazation technique to make the xap file smaller and lazy load assemblies that might contain business logic.

Imagine that you have silverlight that does not require redeployment and only thing you have to redeploy is your reflectively loaded assemblies during runtime!



Step By Step

About project structure

HelloWorld - Silverlight Application responsible for dynamically loading assembly that implements IHello

HelloWorld.Common - Contains interface to be implemented (IHello)

HelloWorld.UserControls - implements IHello that resturns UIElement

HelloWorld.Web - Hosting Silverlight App and it passes DllUrl which is the location of where the assembly to be loaded by Silverlight Application HelloWorld

HelloWorld -> App.xaml.cs

Pass DllUrl initial parameter received from WebApplication to Page control

 

using System;

using System.Windows;

 

namespace HelloWorld

{

    public partial class App : Application

    {

 

        public App()

        {

            this.Startup += this.Application_Startup;

            this.Exit += this.Application_Exit;

            this.UnhandledException += this.Application_UnhandledException;

 

            InitializeComponent();

        }

 

        private void Application_Startup(object sender, StartupEventArgs e)

        {

            this.RootVisual = new Page(e.InitParams["DllUrl"]);

        }

 

        private void Application_Exit(object sender, EventArgs e)

        {

 

        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)

        {

            // If the app is running outside of the debugger then report the exception using

            // the browser's exception mechanism. On IE this will display it a yellow alert

            // icon in the status bar and Firefox will display a script error.

            if (!System.Diagnostics.Debugger.IsAttached)

            {

 

                // NOTE: This will allow the application to continue running after an exception has been thrown

                // but not handled.

                // For production applications this error handling should be replaced with something that will

                // report the error to the website and stop the application.

                e.Handled = true;

                Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });

            }

        }

        private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)

        {

            try

            {

                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;

                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

 

                System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight 2 Application " + errorMsg + "\");");

            }

            catch (Exception)

            {

            }

        }

    }

}

HelloWorld -> Page.xaml.cs

WebClient will download assembly and AssemblyPartwill be used to reflectively load assembly and then UIElement will be added to LayoutRoot.

 

using System;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Reflection;

 

namespace HelloWorld

{

    public partial class Page : UserControl

    {

        private HelloWorld.Common.IHello _helloControl;

        public Page(string dllUrl)

        {

            InitializeComponent();

 

            WebClient client = new WebClient();

            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);

            client.OpenReadAsync(new Uri(dllUrl));

        }

 

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)

        {

            if (e.Error == null)

            {

                AssemblyPart assemblyPart = new AssemblyPart();

                Assembly assembly = assemblyPart.Load(e.Result);

 

                _helloControl = assembly.CreateInstance("HelloWorld.UserControls.HelloWorldControl") as HelloWorld.Common.IHello;

 

                if (_helloControl != null)

                {

                    LayoutRoot.Children.Add(_helloControl.GetHelloWorldControls());

                }   

            }

        }

    }

}

HelloWorld.Common -> IHello

 

using System.Windows;

 

namespace HelloWorld.Common

{

    public interface IHello

    {

        UIElement GetHelloWorldControls();

    }

}

HelloWorld.UserControls -> HelloWorldControl.cs

 

using System.Windows;

using System.Windows.Controls;

 

namespace HelloWorld.UserControls

{

    public class HelloWorldControl : HelloWorld.Common.IHello

    {

        TextBlock _message = new TextBlock();

 

        public HelloWorldControl()

        {

 

        }

 

        #region IHello Members

 

        public UIElement GetHelloWorldControls()

        {

            StackPanel stackPanel = new StackPanel();

            stackPanel.Margin = new Thickness(5);

            stackPanel.Orientation = Orientation.Horizontal;

 

            _message = new TextBlock();

            _message.Height = 52;

            _message.VerticalAlignment = VerticalAlignment.Top;

            _message.TextWrapping = TextWrapping.Wrap;

            _message.Margin = new Thickness(62, 59, 90, 0);

 

            Button button = new Button();

            button.Margin = new Thickness(92,141,158,105);

            button.Content = "Click Me";

            button.Click += new RoutedEventHandler(Button_Click);

 

            stackPanel.Children.Add(_message);

            stackPanel.Children.Add(button);

 

            return stackPanel;

 

        }

 

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            _message.Text = "Hello World!";

        }

        #endregion

    }

}

About project structure

HelloWorld - Silverlight Application responsible for dynamically loading assembly that implements IHello

HelloWorld.Common - Contains interface to be implemented (IHello)

HelloWorld.UserControls - implements IHello that resturns UIElement

HelloWorld.Web - Hosting Silverlight App and it passes DllUrl which is the location of where the assembly to be loaded by Silverlight Application HelloWorld

HelloWorld -> App.xaml.cs

Pass DllUrl initial parameter received from WebApplication to Page control

 

using System;

using System.Windows;

 

namespace HelloWorld

{

    public partial class App : Application

    {

 

        public App()

        {

            this.Startup += this.Application_Startup;

            this.Exit += this.Application_Exit;

            this.UnhandledException += this.Application_UnhandledException;

 

            InitializeComponent();

        }

 

        private void Application_Startup(object sender, StartupEventArgs e)

        {

            this.RootVisual = new Page(e.InitParams["DllUrl"]);

        }

 

        private void Application_Exit(object sender, EventArgs e)

        {

 

        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)

        {

            // If the app is running outside of the debugger then report the exception using

            // the browser's exception mechanism. On IE this will display it a yellow alert

            // icon in the status bar and Firefox will display a script error.

            if (!System.Diagnostics.Debugger.IsAttached)

            {

 

                // NOTE: This will allow the application to continue running after an exception has been thrown

                // but not handled.

                // For production applications this error handling should be replaced with something that will

                // report the error to the website and stop the application.

                e.Handled = true;

                Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });

            }

        }

        private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)

        {

            try

            {

                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;

                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

 

                System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight 2 Application " + errorMsg + "\");");

            }

            catch (Exception)

            {

            }

        }

    }

}

HelloWorld -> Page.xaml.cs

WebClient will download assembly and AssemblyPartwill be used to reflectively load assembly and then UIElement will be added to LayoutRoot.

 

using System;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Reflection;

 

namespace HelloWorld

{

    public partial class Page : UserControl

    {

        private HelloWorld.Common.IHello _helloControl;

        public Page(string dllUrl)

        {

            InitializeComponent();

 

            WebClient client = new WebClient();

            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);

            client.OpenReadAsync(new Uri(dllUrl));

        }

 

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)

        {

            if (e.Error == null)

            {

                AssemblyPart assemblyPart = new AssemblyPart();

                Assembly assembly = assemblyPart.Load(e.Result);

 

                _helloControl = assembly.CreateInstance("HelloWorld.UserControls.HelloWorldControl") as HelloWorld.Common.IHello;

 

                if (_helloControl != null)

                {

                    LayoutRoot.Children.Add(_helloControl.GetHelloWorldControls());

                }   

            }

        }

    }

}

HelloWorld.Common -> IHello

 

using System.Windows;

 

namespace HelloWorld.Common

{

    public interface IHello

    {

        UIElement GetHelloWorldControls();

    }

}

HelloWorld.UserControls -> HelloWorldControl.cs

 

using System.Windows;

using System.Windows.Controls;

 

namespace HelloWorld.UserControls

{

    public class HelloWorldControl : HelloWorld.Common.IHello

    {

        TextBlock _message = new TextBlock();

 

        public HelloWorldControl()

        {

 

        }

 

        #region IHello Members

 

        public UIElement GetHelloWorldControls()

        {

            StackPanel stackPanel = new StackPanel();

            stackPanel.Margin = new Thickness(5);

            stackPanel.Orientation = Orientation.Horizontal;

 

            _message = new TextBlock();

            _message.Height = 52;

            _message.VerticalAlignment = VerticalAlignment.Top;

            _message.TextWrapping = TextWrapping.Wrap;

            _message.Margin = new Thickness(62, 59, 90, 0);

 

            Button button = new Button();

            button.Margin = new Thickness(92,141,158,105);

            button.Content = "Click Me";

            button.Click += new RoutedEventHandler(Button_Click);

 

            stackPanel.Children.Add(_message);

            stackPanel.Children.Add(button);

 

            return stackPanel;

 

        }

 

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            _message.Text = "Hello World!";

        }

        #endregion

    }

}



Demo


 

 

Privacy Statement  |  Terms Of Use
Copyright 2009 by New Age Solution Inc.