October 7, 2009
October 3, 2009
Note: This is part of a series, you can find the related posts here…
Configuration of ASP.NET applications is quite potential. Simple application settings, database connection strings, complex configuration data. All editable by the administrator after deployment.
Configuration of SL applications is virtually non-existent.
While a Silverlight application may have less need for configuration than a web application – part of the logic still remains server side in respective services anyway – it’s not as if there is no need at all.
The service URLs (or a base URL) themselves, for example. Service proxies are created with the very URL that was used to provide the meta information baked into the code. Since this is probably a local address on the developers machine it needs to be adjusted during runtime. And preferably the administrator should be able to reconfigure the SL application if the service URL changes.
Other use cases include the usual: Whatever you want to put in some appSettings replacement. Enabling debug features. Just have a look at your last web application’s configuration and you’ll probably find some good candidates.
Looking at the object tag for a Silverlight control on a web page (the ASP.NET server side control we had with SL2 is gone with SL3), there is some technical parameterization available:
You can find a complete list of available parameters here and below.
Of particular interest right now is the initParams property. It allows me to pass arbitrary information to the SL client. It is however not suited to be maintained by an administrator, as I certainly don’t want him to mangle my web page. However nobody says I cannot provide code that reads the information from somewhere else, like so:
and the respective code, obviously intended to read in the contents of a config file:
That config file is a simple analogon to the standard ASP.NET appSettings:
Note: I could have returned the web.config appSettings section. Yet using a separate file helps keeping client and server configuration cleanly separated.
So, what’s left is the actual implementation of that InitParams class, I’ve been using…
The InitParams class is merely a little helper, providing a fluent interface over a dictionary and convenience methods, as I needed them:
Reading the config file is equally simple with XLinq at our disposal:
And I will complement that with additional helpers in upcoming posts.
What’s it worth?
So now I have the ability to provide configuration to my client, the information readily available to be picked up during the startup event or anytime from Application.Host. For simple values I have the configuration file, for other information I can extend my helper class anytime I want (and I will!), the infrastructure is there.
Could that have been done otherwise? Well, a respective services would have been another option. But providing the information via the initparams has the advantage of being readily available to the client without delay. And configuring the configuration service via the configuration service…? Well.
That’s all for now folks,