I am developing a silverlight 4.0 application which communicates with a WCF service.
The WCF configuration (endpoint, …) is contained in the ServiceReferences.ClientConfig file.
However, I would like the silverlight application to get this config from the web server as I don’t want to recompile the application or fiddle with the XAP file (I know it’s a zip file) since the users are only allowed to configure the web server and this config would be different per server/site.
Is there a way for a silverlight application to receive some kind of info from the web server or to get such data from it?
Ok, after much searching around the net I was able to code the following solution by aggregating the different pieces of information I found.
First, Silverlight 4 (like SL3) use a different mechanism for creating the silverlight control in the web page on the client. It uses the < object > tag.
To pass initialization parameters to a silverlight application you just need to add
to the page.aspx file (from the web project) under the object tag and the SL app will receive those 2 parameters at startup.
For example, using the default aspx page generated from VS2010:
To access the parameters in the SL app, you just have to code the following in the App.xaml.cs file:
For now, this only allows static values for the parameters.
To have dynamic values you just have to change the initParam line to:
to get the values from the configuration file on the web server. 😉
I hope this helps some poor soul out there!