Customization of Silverlight application from web.config

by Augustin Šulc

We came across a situation when we needed to configure a Silverlight application through AppSettings in web.config. Since every Silverlight application runs on client, it cannot read the file directly. There was an idea to create a web service that would pass the configuration parameters to the Silverlight client, but this seemed as overkill to me.

I came up with another solution. The core idea is:

  • Set the parameters in the AppSettings node in web.config.
  • Pass the parameters through the <params> section of the tag where the Silverlight application is defined (usually default.html).
  • Read the parameters in the Application_Startup handler.

There are probably other ways to achieve the goal but this one works well (at least for us). We can customize the application without rebuilding it, only by changing web.config. I hope you would find this idea useful too!

Detailed implementation follows:

  1. Set the parameters in web.config:
      <appSettings>
        <add key="Client" value="GiusCZ" />
      </appSettings>
  2. Pass the parameters to the Silverlight application (default.aspx page):
    <body>
        <form runat="server" style="height:100%">
        <div id="silverlightControlHost">
            <object id="xaml" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
                ...
                  <param name="initParams" value="Client=<%= System.Configuration.ConfigurationManager.AppSettings["Client"] %>" />
                ...
        </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
        </form>
    </body>
    
    
    More information can be found here.
  3. Use the parameters in App.xaml.cs:
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        ApplyClientCustomization(e.InitParams["Client"]);
    }

Tags: Silverlight

2 Comments

  • Danilo Chaves R. said

    Excellent. it works fine.

  • best essay said

    I recently ran over your blog and have been reading along. I figured I would leave my first comment. I might want to thank you for the efforts you have made in writing this article. I trust a similar best work from you later on also. Truth is told your creative writing capacities have motivated me to begin my own blog now.

Add a Comment