Ever felt the need to have something like a web.config-file, with settings you can change when they need to be changed without requiring a rebuild (eg: number of results in a grid, endpoints of certain services, certain ID’s, …)? I know I have.
But by default, this isn’t so easy in Silverlight. Point is, Silverlight runs on the client, so changing a config-file on the server would have no effect on the way the application runs. You could, off course, just add an XML-file with some settings to your XAP-file – this file & corresponding settings would be available on the client, ’cause it’s included in the XAP. But you won’t be able to change these settings on the fly (ie, once the client has downloaded your XAP, the settings will stay as they are), and more importantly: changing your settings would require a rebuild & re-rollout of your XAP-application.
Wouldn’t it be nice if we could just access settings from the web.config of the web application hosting the Silverlight-control, which we can easily change when we need to (eg, change in requirements or rollout to a different environment), without rebuilding & on the fly?
Well, with a little bit of work, we can do just that! 🙂
1) create a web service in the webapp hosting your SL-app, which will read an entry from the web.config file and pass it back to the caller. I’ve called mine "ConfigurationReader", and it’s a simple ASMX-service.
1: [WebMethod]
2: public string GetApplicationSetting(string ApplicationSetting)
3: {
4: return ConfigurationManager.AppSettings.Get(ApplicationSetting);
5: }
2) add a service reference to this webservice to your SL-app.
3) as the ConfigurationReader-service can change location (it won’t always be in the same location as it was when we added the reference!), it’s necessary to pass this location to the Silverlight-app somehow. And to take it 1 step further, I’d like to make sure I can easily change this location, so I make it a key in the web.config.
How? On your aspx-page, you’ll see there’s an asp:Silverlight-control, hosting your SL-app. This control has a certain property, "InitParameters". This is a very easy way to pass information from your aspx-page to your SL-app. So, in the codebehind of the aspx-page, we set the InitParameters equal to the key in the web.config containing the location of the ConfigurationReader-service
1: protected void Page_Load(object sender, EventArgs e) {
2: // get the correct URL of the configservice and pass it to the SL-app by using InitParameters
3: Xaml1.InitParameters = "ConfigurationReaderURL=" + ConfigurationManager.AppSettings.Get("ConfigurationReaderURL"); }
1: private void Application_Startup(object sender, StartupEventArgs e) {
2: // get the service URL
3: Helper.ConfigurationReaderServiceURL = e.InitParams["ConfigurationReaderURL"];
4: this.RootVisual = new Page(); }
1: // initialize service reference with correct URL, add handler to completed event and call the service
2: BasicHttpBinding binding = new BasicHttpBinding();
3: EndpointAddress endpoint = new EndpointAddress(Helper.ConfigurationReaderServiceURL);
4: ConfigReaderServiceReference.ConfigurationReaderSoapClient client = new ConfigReader.ConfigReaderServiceReference.ConfigurationReaderSoapClient(binding, endpoint);
5: client.GetApplicationSettingCompleted += new EventHandler<ConfigReader.ConfigReaderServiceReference.GetApplicationSettingCompletedEventArgs>(client_GetApplicationSettingCompleted);
6: client.GetApplicationSettingAsync("Setting1");
And that’s it! Now you have a system in place with which you can easily use application settings you can get when you want and/or change on the fly (try it by changing a setting in your web.config after the SL-app has been loaded, for example), without having to rebuild your SL-app or anything!
I’ve included full sourcecode for this for those who are interested. Enjoy!