Software Architecture – Passing WCF Service reference URL to Silverlight initialization paramters via webconfig of Web project

Visual Studio 2010 + .Net 4.0 + Silverlight 4.0 + WCF

Problem definition:In Silverlight application,normally there are 2 projects: one of Silverlight project which runs on client side as Silverlight is smart client technology. Second Project is project which runs at server side. Data layer is residing on Server side in ASP.Net project. In order for to communicate b/w Silverlight and ASP.Net , WCF is used.

So there is a WCF Service in ASP.Net project and a service reference in Silverlight project.
So it is often need to change Service URL to production Server. So dynamically passing service url to silverlight project is required.

Normally web.config of ASP .Net project is used to manage the Service reference URL of Silverlight project.

• Create a normal silverlight solution, so it will contain one silverlight project and one ASP .net project.
• Add a configuration entry for service reference url in web.config of ASP .Net project as:

<add key="ServiceURL" value="http://localhost/PassingInitialParameters.Web/WCFService.svc" />

• Open ASP .Net Page hosting the silverlight object and change the Silverlight object as following to add a initialization parameter.

<object id="SilverlightObject"  name="SilverlightObject" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
		  <param name="source" value="ClientBin/PassingInitialParameters.xap"/>
		  <param name="onError" value="onSilverlightError" />
		  <param name="background" value="white" />
		  <param name="minRuntimeVersion" value="4.0.50401.0" />
          <param name="initParams" 
                value="ServiceURLForSLApp=<%= ConfigurationSettings.AppSettings["ServiceURL"]     %>"/>
		  <param name="autoUpgrade" value="true" />
		  <a href="" style="text-decoration:none">
 			  <img src="" alt="Get Microsoft Silverlight" style="border-style:none"/>

So as ASP.Net page loads, it will fetch service reference url from web.config and assign in initialization parameter named ServiceURLForSLApp.

• Lastly in App.xaml.cs of Sliverlight project add, code for getting initialization parameters and creating a end point address with service url and finally create service client object.

private void Application_Startup(object sender, StartupEventArgs e)
            string ServiceURL = e.InitParams["ServiceURLForSLApp"];
            BasicHttpBinding binding = new BasicHttpBinding();
            EndpointAddress endpoint = new EndpointAddress(ServiceURL);
WCFServiceReference.WCFServiceClient client = new WCFServiceReference.WCFServiceClient(binding, endpoint);


Call Service client object as needed to call method of WCF service for fetching data.

Code for sample project is attached for reference.
Rename file as by removing .doc extension to extract the code solution.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: