Creating and Using Silverlight web.config app settings (Web.config Configuration Applicatioin Settings) or app.config application settings configuration file for Silverlight

Post Summary:

This posts explains how to create and use something similar to a silverlight  web.config, App.config and appsettings (or app settings) configuration file for silverlight application. This is based on re-using serviceReference.clientconfig as an appconfig configuration file.

The Problem:

A) configuration application settings (like the one in web.config, appsettings or app settings application configuration file):
This is probably very common. Your silverlight application might well need to have some configuration settings that you can change their values without having to re-build and re-deploy the whole application because of a small change in a connection string or appSetting value. Again, AppSettings in good old web.config files in applications were the best solution in this case.

B) WCF Address (EndPoint address value) change after building:
Another reason for wanting to have a configuration file in a silverlight application: Have you ever wanted to change the WCF service address (endpoint address) in your silverlight application after deployment. Imagine that you built a silverlight application and deployed it on your test server. The application was tested and confirmed issues and bug free. Now you want to re-deploy the application to the Live server but the WCF service address (end point address) is different which is probably quite common. It might also be that the address is the same but the port on which the WCF service is listening is different. In this case, you will need to change the value of the WCF endpoint address and re-build. So what happened to configuration files! They are a standard now in all Microsoft environments.

Problem Summary: The problem is there is no clear web.config file in silverlight as the whole code is compiled into a (.xap) file which any html or aspx page can call.


In my silverlight application, I had a connection string value that I need to use in my silverlight application (pass it to the webservice). Hence, I wanted the flexibility to change the WCF endpoint address and the connection string configuration value (or any other application setting). I started looking for a solution but I found that most work arounds were either t0o long or too complicated. As always, I like simple things and I don’t like over complications. I have to say though that most if not all work arounds that I found were useful for me to come up with this solution. I am not sure but I believe (and hope) the following solution is unique as I have searched for quite a while to find anyone suggesting this idea but I couldn’t find any.

Solution part 1: The simplest way for adding application settings to a silverlight application.

Now since ServiceReferences.ClientConfig is just an xml configuration file, you can actually edit it in visual studio and simply add your application settings.

Your file will look something like this:

                <binding name=”xrmServiceBasicHttp” maxBufferSize=”2147483647″
                    <security mode=”None”>
                            <extendedProtectionPolicy policyEnforcement=”Never” />
            <endpoint address=”http://localhost:1234/MyWCFService.svc” binding=”basicHttpBinding”
                bindingConfiguration=”xrmServiceBasicHttp” contract=”XrmService.IXrmService”
                name=”xrmServiceBasicHttp” />
<add key=”ConnectionString” value=”data source=ABC01;initial catalog=DB01;integrated security=SSPI;persist security info=False;packet size=4096″ />  

Now that you have added your application setting (connection string) to your ServiceReferences.ClientConfig, you need to use it in your silverlight code behind (.xaml.cs) file.

Remember, ServiceReferences.ClientConfig is just an XML file (already said that many times!). Hence, you can write a very simple and straight forward function that parses your ServiceReferences.ClientConfig xml file and returns the appSetting value of your application setting.
A function to return an AppSetting value from your clientconfig file can look something like this:

private string getAppSetting(string strKey)
            string strValue = string.Empty;
            XmlReaderSettings settings = new XmlReaderSettings();           
            settings.XmlResolver = new XmlXapResolver();
            XmlReader reader = XmlReader.Create(“ServiceReferences.ClientConfig”);
            while (reader.Read())
                if (reader.NodeType == XmlNodeType.Element && reader.Name == “add”)
                    if (reader.HasAttributes)
                        strValue = reader.GetAttribute(“key”);
                        if (!string.IsNullOrEmpty(strValue) && strValue == strKey)
                            strValue = reader.GetAttribute(“value”);
                            return strValue;
            return strValue

Finally in your code, to retrieve the AppSetting value for the appSetting key (ConnectionString) that you created, you can just call the above function like this:


And One last thing, remember when you create the WCF service client object, don’t explicitly call the WCF service address, as it is already in your ClientConfig file.

So, just call the end Point Configuration Name (endPointConfigurationName) that you want to use. Just like this:

MyServiceClient client = new MyServiceClient(“MyServiceBasicHttp”);


Solution part 2: Changing WCF address and endpoint values in “ServiceReferences.ClientConfig”.

Anyway, the first thing we need to do is to understand that “ServiceReferences.ClientConfig” that all silverlight applications have and use if they are connecting to a WCF sercvice, is just a simple XML file. Actually, not only an XML file but also a configuration file for the application.

You also need to know that the silverlight xap (*.xap) file that silverlight generates is a simple compressed (zipped) file. For this reason, you actually can rename the file to ( from (something.xap) and then you can access its content. The content of the xap file includes AppManifes.xaml, your application dlls and most importantly “ServiceReferences.ClientConfig”.

Now, when I tried to unzip it, change the content of one of its files and then re-zip it and rename it back to (something.xap), this has corrupted it. My application failed. I am not sure why. This was the case despite the fact that most posts about unpacking, unzipping and re-packaging the .xap file works.

What actually works is the following:
Rename the something.xap file to file. Open the zip file without unzipping (just double click on it) and then copy the file you want to edit, which in our case is the “ServiceReferences.ClientConfig” and paste it in a different location. Edit it and make all the changes you need to it and then copy it and paste back into the file. Replace the exisitng file there. Now go to and rename it back to something.xap.

Whatever change you have made should now work.


I hope this helps. I really think it is an effective way to get your AppSetting values.

If you got any questions, please comment below (Kindly don’t use the contact page for questions on posts please)

Also, please let me know (via a comment below) if you want a copy of this application code.

11 Replies to “Creating and Using Silverlight web.config app settings (Web.config Configuration Applicatioin Settings) or app.config application settings configuration file for Silverlight”

  1. Wow! This post really made my life easy. I could ‘ve been struggling for hours without it.. I can’t believe Microsoft didn’t provide us a simple straight forward solution. Your work around “invention” is great though!

  2. Good one the client config code works well for me
    I have to read the customize data (size of rows to be dumped in db)…
    which I am doing using client config by.

    in client config.
    can’t we write the same code in web.config of wcf and access it at
    client side silverlight hello.xaml.cs code.

  3. I am running my Silverlight application through html file not aspx file. So how can i use web.config in my .xaml.cs file to access some application level values.

Please comment or leave feedback