Latest Tweet:
  • Loading...

On the Silverlight.net forums there are several questions about how to configure Silverlight applications. A common question is how to change the URL of a WCF service you are consuming. Another common question is how pass in some user defined configuration settings. There are several interesting solutions, and a couple of days ago Bill Reiss posted an article about “Calling WCF on the server of origin from Silverlight”. Bills solution involves using the HTML Bridge to grab the current URI, and building up the URL of the web service, and then creating the service client using one of the constructors allowing you to manually specify binding and endpoint. This is all cool and really helpful in many cases, but the ServiceReferences.ClientConfig file is there for a reason. I think a lot of developers are a bit confused about how to use this file. And I don’t blame them. In Silverlight 2 Beta 1 this file was generated by Visual Studio, but was not read by the WCF runtime. In Beta 2 this was fixed, and the WCF runtime will now read and use the configuration file.

A lot of developers are afraid they have to rebuild their application to change web service configurations; something Bill also write in his blog post:

This is fine if you're hitting a service like Digg or one of the other public web service APIs out there, but not if you want to be able to test on your own machine and also deploy the same XAP to your server without rebuilding.

You could go ahead and change this to the URI on your server and rebuild, but then it won't work locally for testing unless you have a crossdomain file on your server, and you would be hitting your server's web service, not your local one while testing.

When building a Silverlight 2 application Visual Studio 2008 will spit out a XAP file in the Client Bin directory. The XAP file is just a standard ZIP archive containing some assemblies, the configuration file, a manifest file, and any resources you might have added to your project and market as content. It’s completely safe to rename it to ZIP, unpack it, make changes to the ServiceReference.ClientConfig file and package it all back together to a XAP file. Turns out repacking your XAP file is actually a good idea. Back in May Valeri Hristov discovered that he could chop 1MB of a 3MB large Telerik example application by repackaging it using the 7Zip application. Later David Anson followed up with some more examples showing a 22% average saving by repacking the XAP file… Why Microsoft is using such an inefficient ZIP implementation I don’t know –hopefully this will be fixed before release. Until then creating a post-build event that repackages your XAP will probably be the easiest 22% savings you will get all day.

Content of a XAP file

Anyway, let’s get back to configuration. Now that we can reconfigure our Silverlight application without rebuilding it we might be able to use the configuration file for more useful stuff. Silverlight does not come with a full configuration API like the full .NET framework. You do have the System.Configuration namespace, but it only contains some internal, abstract classes used by the Silverlight WCF stack. If you search for “configuration” in Reflector targeting Silverlight 2.0 you get some hits in the System.ServiceModel.Configuration namespace as well. It got configuration section handlers used by WCF to read information about your web service bindings and end points. All classes are internal and not really helpful for handling general application configuration.

Since the ServiceReference.ClientConfig is a plain XML file there is nothing stopping us from adding our own elements to the file. I decided to use the application setting convention we’re used to from .NET, and added an appSettings element with a bunch of key-value pairs elements. The application continued to run just fine with these additional elements. I also tried adding and removing service references from Visual Studio to see if it would screw up my changes. Thankfully Visual Studio only touched the serviceModel element, and didn’t care about our appSettings element. The ServiceReference.ClientConfig file now looks something like this:

configsample1

This is quite handy, as we can keep all application settings in one file using a format developers know. Next step is writing some code to read the configuration file. I decided to mimic how things work in the full .NET framework by creating a ConfigurationManager class with an AppSettings property. To parse the configuration file I used LINQ to XML which made it really easy to extract the key-value pairs.

configsample2

In the full framework the AppSettings property is of type NameValueCollection. This collection does not exist in Silverlight, so instead of re-implemented it I created a simple class backed by dictionary, exposed as an index property.

configsample3 

You read the application settings the same way you are used to from any .NET application:

configsample4

Hopefully this post gives you a better understanding how Silverlight 2 applications are packaged, and how to configure your application without rebuilding it. If I was consuming WCF services in my project I would add configuration management to my automated build process. It would be fairly simple to write a build script that repackages the XAP with a more efficient ZIP implementation, as well as building different versions for test, staging and deployment.

Tuesday, August 26, 2008 9:57:44 PM (W. Europe Daylight Time, UTC+02:00)
I have seen the light! And I know what color it is!
Espen
Wednesday, August 27, 2008 1:40:18 AM (W. Europe Daylight Time, UTC+02:00)
Simple and very functional. I like it!

Pete
Monday, October 20, 2008 3:58:15 PM (W. Europe Daylight Time, UTC+02:00)
Amazing example.but the problem is after changing the appsetting the silverlight application is not working.(i.e showing blank screen). am i missing something? can u please help me to resolve this issue?

i added a web application for your silverlight application.after tht i deployed the application in iis, its working fine, after that i extracted the xap -> zip , i modified clientconfig, again i converted to xap after it is showing blank screen in web application.

Monday, October 20, 2008 5:03:59 PM (W. Europe Daylight Time, UTC+02:00)
Hi,

I have followed the steps mention, for the changes made in configuration manager file. But it displays blank screen.

I couldn't understand the reason.

regards
sridhar
Thursday, October 23, 2008 1:28:50 AM (W. Europe Daylight Time, UTC+02:00)
Sudhir/Sridhar: Interesting... I have to look into this, as this example was written for Silverlight 2 Beta 2, and there might have been some changes. Let me get back to you guys when I have figured out if there is any Silverlight 2 RTW issues.
Sunday, November 02, 2008 7:11:34 PM (W. Europe Standard Time, UTC+01:00)
Great post, Jonas.

The following works with RTW:

namespace System.Configuration
{
public static class ConfigurationManager
{
public static Dictionary<string, string> AppSettings { get; set; }

static ConfigurationManager()
{
var doc = XDocument.Load("ServiceReferences.ClientConfig");

IEnumerable<XElement> descendants = doc.Descendants("appSettings");

Dictionary<string, string> dictionary =
descendants.Descendants("add").ToDictionary(
e => e.Attribute("key").Value,
e => e.Attribute("value").Value);

AppSettings = dictionary;
}
}
}
Zac Morris
Sunday, November 02, 2008 10:24:36 PM (W. Europe Standard Time, UTC+01:00)
Awesome Zac - Thanks for sharing an updating sample on parsing the XML file. Glad you found the post useful :)
Tuesday, January 06, 2009 2:21:51 PM (W. Europe Standard Time, UTC+01:00)
Hey Jonas,

Nice article.

On my machines, I set the "xap" extension to be opened by WinZip by default. This way I can just double click the XAP, or even open it directly from Visual Studio (using Open With in the Solution explorer).

I like your solution a lot. I will contact you directly with a request.

Cheers,
Laurent
Thursday, April 16, 2009 11:08:21 PM (W. Europe Daylight Time, UTC+02:00)
Very nice and helpful article. I able to read the file successfully. Thanks a lot.
Monday, June 29, 2009 1:19:43 PM (W. Europe Daylight Time, UTC+02:00)
Thanks a lot for the Code
Works like a Charm...
You saved me a lot of stressful coding...

Best Regards

Dani
Danish Ali
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910