In asp.net application we often use to save the connection string and some other information like user name , web service URL etc in the web.config and then in application read them and use in the application and even we can easily changes these value at later time.However, this scenario does not work out of the box from a Silverlight application, which doesn’t seem to even have access to web.config.The reason is simple: a Silverlight application runs on the client side. The XAP -just like the browser- cannot access server resources such as web.config. In this post I will show you how you can read the web.config and pass to the silverlight application and then on the silverlight application how can you read the passed parameters.Passing Parameters :
To pass the web.config parameter or other parameter to the silverlight is very easy you can pass the web.config or other value by assigning the InitParam parameter. For this you have to read the web.config values and store these values in the key value format.The code listed in the List 1 show how to read the value from the web.config and paired them in key value format. Here you can see that I have read two values from the web.config one is the connection string and the second one is the user name. You see that each key value pair has equal to (=) sign between them.
And for second value which is the user name you can see that comma (,) sign. as the comma(,) sign indicate new key value pair.And at the last statement which is written in the html side to pass the parameter to the silverlight application. Here you can see I have used the InitParams InitParams is one of the param name of the Silverlight plug-in object. Generally it is a dictionary object of type string. It contains a set of user defined key/value pairs. By default it is set as null or empty string and initializes at the time of first instance.
Reading InitParams :
In this post I will show you 2 ways to read the InitParams on the silverlight application.
1- Application_Startup :
you can read the passed value in the Application_Startup event handler which is written in the App.xaml.cs file. The code to read the passed values on the silverlight application at the Application_Startup event handler is listed in List 2. Here you can see that StartupEventArgs e has property InitParams which is ready only of type IDictionary which takes two value of type string. Here I have first check the key which is the connection string and the user name and then I have read these values in the WebDotConfig class which has two properties with the ConnectionString and the UserName which are static.
The reason for reading the values in the Application Startup event handler is the some time you need to use the Web service URL in you application. It is better to read it here and store it in some class which is accessible later when you need to use same URL to authenticate the user name. As Connection string and user name are static so one there are read you can use them any where in you application.
1- SilverlightHost :
In you don't the the variable which are used at the application start up like the web service URL which is used in common scenario. You can also read the passed parameter to the silverlight application by using the SilverlightHost class which has the InitParams property which is discussed in the Application Startup event handler.
In List 3 you can see I have used the InitParams of the SilverlightHost class and read the key values and assign them to the control which are used to display the passed parameter values. The output can been see in the Image 1, here I have displayed values read from both way from the Application startup event and also from the SilverlightHost class.
I have separated both the section and place the Header for Application Statrup (Which displays values read at the Application Startup event handler and save in the WebDotConfig class, I have used binding for this as I have class which contain properties and bind to the respective controls on the user control)and for Host InitParams (Which are set at the code behind as I am reading values in the User control loaded event handler). .You can download the source code from here.
All and any comments / bugs / suggestions are welcomed!