ASP.NET MVC 2 - Passing Parameters to Silverlight application

In our SDMS application (source code here) we pass parameters from our ASP.NET MVC 2 application to our Silverlight application.   We have a ISystemConfiguration implementation that contains the following information from our ASP.NET MVC 2 Web.Config file (watch window below).  I should note here that we'll have one configuration file to manage our entire application (MVC 2 and Silverlight).


Since config on line 78 is a singleton we have effectively updated our unity container to contain the same parameter object that our MVC 2 application contains (the contents of it's Web.Config file).   See THIS BLOG for more information on the Serialization/Deserialization process.

You'll find in the Library.Demo.ASPMVC2 application of our SDMS solution that we can easily upgrade our MVC 2 application to have Unity support by merely changing the classes that the Global.asax and Controllers derive from using our MVCContrib project (see the LibraryDoc.chm file for details).   Unity allows us to easily support Multi-targeting which means our ASP.NET MVC2, WPF and Silverlight applications all share the same codebase.   Below we see the only Silverlight specific code that was required in our Bootstrapper class was to deserialize the XML String back into our ISystemConfiguration object.   We don't have to do this in WPF (which also shares this code) because it's Unity container already contains it . 

 

Note for PRISM readers: We have to configure our container, and deserialize our parameter object, in GetModuleCatalog because it is called from the UnityBootstrapper's CreateContainer command which occurs prior to CreateShell (see sequence diagram here); we want to ensure we have everything registered before we buildup our view, presenter and call the presenters OnGetModuleCatalog() where the application specific work is done.  

An important part of passing our parameter object is to first make it available to MVC 2.   You'll find that we didn't have to make any changes to a default MVC 2 application (outside of the classes derived from) which indicates that the MVCContrib infrastructure did all of the work of pulling the configuration information out of the Web.Config file into our parameter object for us.  

Below you'll see that we tap into the Master page to call our own html extension SetContainer() on line 57 of the top window in the image below.   Our extension simply takes the parameter object, Serializes it using our object extension, and then passes the resulting xml string into ViewData["InitParams"]



Next we have to get our ViewData["InitParams"] that was set in line 54 above (bottom window) into Silverlight.  The following excerpt from our Library.Demo.ASPMVC2\Views\Home\Silverlight.aspx shows us how this is done:

As a result of the above the Silverlight's App.xaml.cs file will receive the serialized parameter (xml string) in it's Application_Startup method on it's StartupEventArgs (e.InitParams).

private void Application_Startup(object sender, StartupEventArgs e)
{
    BootStrapper bootstrapper = new BootStrapper(new MainPage(), e);
    bootstrapper.Run();
    // The following was moved into MainPage.Show()
    // => this.RootVisual = new MainPage();
}

We'll pass this into our Bootstrapper which it will store it in an args field which brings us full cycle to our first image where our serialized string is retrieved via 

      var para = args.InitParams["InitParams"]

private StartupEventArgs args;
public BootStrapper(object sender, StartupEventArgs e)
{
    view = sender
as IShellView;
    args = e;
}


Tags: , , ,
Categories: Unity


Actions: E-mail | Permalink |  Grammar/Typo/Better way? Please let me know