Sunday, November 7, 2010

Reading Web.Config to pass values to silverlight Application

In 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.

< script runat="server"> public string WebDotConfig { get; set; } protected void Page_Load(object sender, EventArgs e) { WebDotConfig = "ConnectionString=" + ConfigurationManager.AppSettings["ConnectionString"]; WebDotConfig += ",UserName=" + ConfigurationManager.AppSettings["UserName"]; } < /script> //passing value <param name="initParams" value="<% =WebDotConfig%>" />
List 1
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.

private void Application_Startup(object sender, StartupEventArgs e) { if (e.InitParams != null) { if (e.InitParams.ContainsKey("ConnectionString")) WebDotConfig.ConnectionString = e.InitParams["ConnectionString"]; if (e.InitParams.ContainsKey("UserName")) WebDotConfig.UserName = e.InitParams["UserName"]; } RootVisual = new MainPage(); }
List 2

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.

private void UserControl_Loaded(object sender, RoutedEventArgs e) { if (Application.Current.Host.InitParams.ContainsKey("ConnectionString")) txtConnectionString.Text = Application.Current.Host.InitParams["ConnectionString"]; if (Application.Current.Host.InitParams.ContainsKey("UserName")) txtUserName.Text = Application.Current.Host.InitParams["UserName"]; }
List 3

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.

Image 1

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!


Sudhir Yadav said...

But what if this application is running out of browser. I believe and have tested that it will not work in our of browser e.InitParam count is 0.

Chad said...

Excellent post Asim! I have been scouring the internet for a solution to this and nothing worked until I found this, thank you!

Anonymous said...

excelent... thanks

aparna john said...

Hi,If your site takes like forever to load, then probably it is either composed of large file sizes for Web Design Cochin or your hosting account is not doing its task in delivering the web pages properly. Thanks.............