lundi 4 août 2008

Read web.config parameters from a Silverlight application

Silverlight is an RIA so the application only live on the client side. Sometime it's a good idea to store parameters on the web.config file (i.e a web service endpoint).

Of course it's impossible to read the configuration file directly from the application running on the client.

What's the workaround ?
The Silverlight application is host on an aspx page in a control. So it's possible to get a web.config item on the page loading using the ConfigurationManage :

string _baseUrl = ConfigurationManager.AppSettings["EndPointAdress"];

After you must give this parameters to the Silverlight control, you can do that via his InitParameters property :

SLControl.InitParameters = "EndPointAdress=" + _baseUrl;

Last step : get the parameter in the Silverlight application. You must do this step in the App.xaml.cs file, in the Application_Startup method :

private void Application_Startup(object sender, StartupEventArgs e)


string _baseUrl = e.InitParams["BaseUrl"];


That's all ! :)

Aucun commentaire: