Configuration in Silverlight

Configuration Options

ConfigurationManager, the commonly used .NET configuration API, is unavailable in Silverlight applications. The minimum code option is to deserialize an XML configuration file, or to read from initParams.

Use MSBuild to merge XML deployment configurations

While most Silverlight applications only need Service endpoints updated in ServiceReferences.ClientConfig, there are occasions where a PRISM module may be needed to support reconfiguration. When this happens, I use an MSBuild task to create and/or merge the correct settings into a module.xml within the PRISM module, using a strongly typed object representing the XML file. I also deserialize the XML file to retrieve its values.

Strongly typed object representing its XML file

          [DataContract(Name = "UserModuleConfiguration")]
public class UserModuleConfiguration : INotifyPropertyChanged
{
    private String _employeeUploadServiceUrl;
    [XmlAttribute]
    public String EmployeeUploadServiceUrl
    {
        get { return _employeeUploadServiceUrl; }
        set { _employeeUploadServiceUrl = value; RaisePropertyChanged("EmployeeUploadServiceUrl"); }
    }



    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

        

Deserialize the XML file to retrieve its values

          <usermoduleconfiguration employeeuploadserviceurl="http://localhost:62509/Services/UserModule/UserModuleUploadHandler.ashx">
</usermoduleconfiguration>

    public class UserModuleConfigurationRepository : IUserModuleConfigurationRepository
    {
        public UserModuleConfiguration GetSection()
        {
            var sr = Application.GetResourceStream(new Uri("/UserModule;component/Configuration/UserModuleConfiguration.xml", UriKind.Relative));
            var transform = new XmlSerializer(typeof(UserModuleConfiguration));
            return (UserModuleConfiguration)transform.Deserialize(sr.Stream);
        }
    }

        

Use initParams in standalone XAPs

Another approach to configuration in Silverlight is with initParams. I only use this option sparingly, as complexity and hierarchy cannot be as easily expressed in key value pairs as they can with XML. Wherever a Silverlight application, XAP, is being used for reusable standalone functionality, then initParams is an excellent way to enable configuration at run time.

The only production examples of initParams that I have are for a standalone reusable Silverlight video player and file uploader. The following section shows how we used initParams to build a reusable file uploader, to integrate with our ASP.NET MVC application.

Each HandlerUrl and ServiceUrl is specific to each instance.

          <object data="data:application/x-silverlight-2," height="400" type="application/x-silverlight-2" width="600">
    <param name="source" value="/ClientBin/RobustHaven.Silverlight.App.xap" />
    <param name="onError" value="onSilverlightError" />
    <param name="background" value="White" />
    <param name="minRuntimeVersion" value="4.0.50401.0" />
    <param name="autoUpgrade" value="True" />
    <param name="initParams" value="HandlerUrl=http://localhost:6387/Products/WcfServices/FileUploadHandler.ashx,Key=1,ServiceUrl=http://localhost:6387/Products/WcfServices/FileUploadService.svc" />
    <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0" style="text-decoration:none;">
    <img alt="Get Microsoft Silverlight" src="http://go.microsoft.com/fwlink/?LinkId=161376" style="border-style:none;" /></a>
</object>

        

The Silverlight configuration repository actually reads the initParams.

          using System.Windows;
using RobustHaven.UploadFiles.Entities;

namespace RobustHaven.UploadFiles.Providers.InitParams
{
    public class UploadConfigurationRepository : IUploadConfigurationRepository
    {
        public UploadConfiguration GetSection()
        {
            return new UploadConfiguration() {
                ServiceUrl = Application.Current.Host.InitParams["ServiceUrl"],
                HandlerUrl = Application.Current.Host.InitParams["HandlerUrl"],
                Key = Application.Current.Host.InitParams["Key"]
            };
        }
    }
}