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

Environment:
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 ASP.net 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.

Solution:
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:

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

• 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="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0" style="text-decoration:none">
 			  <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
		  </a>
	    </object>

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 PassingInitialParameters.zip by removing .doc extension to extract the code solution.
References:
http://msdn.microsoft.com/en-us/library/ms733749.aspx
http://msdn.microsoft.com/en-us/library/aa480210.aspx

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: