Tutorials » Silverlight tutorials »

How to call WCF methods from Silverlight controls ?

This tutorial and the attached sample project demonstrates how to access server side data from Silverlight controls using WCF.

Silverlight controls get executed on the client browser. It does not have direct access to the data on the serverside. So, if your Silverlight controls need to retrieve data from database or other data sources on the server, we have to use various approaches like WCF calls or depend on the InitParameters property of the Silverlight controls.

The recommended approach to get server side data to Silverlight controls is using WCF method calls. The most important benefit is the type safety.

The following sample demonstrates how to use WCF to retrieve data from Server side to Silverlight controls on the client side.

In this sample, we will create a Silverlight project and another web project to host the Silverlight control. Also, we will add a WCF Service to the web project.

Silverlight with WCF sample

Create a new Silverlight project with a web project to host the Silverlight control.

Open Visual Studio and select the menu "File" > "New" > "Project"
Select the Project Type as "Silverlight" under your favorite language and choose the template "Silverlight Application". I have selected Visual C# as the tutorials here.

I have named my project as "MySilverlightApp" and have selected the option "Create directory for solution" so that all my project files are organized within a folder structure.

In the next screen, choose the option "Add a new web to the solution for hosting the control".

Right click on the web project in the Solution Explorer and select "Add New Item".

Select the category "Silverlight" from the left panel in the dialog box.

From the right panel, select the template "Silverlight-enabled WCF Service"

Choose the default name "Service1.svc" and press the "Add" button.

(The current beta version of Visual Studio may show you an error object reference not set to an instance .... Ignore this error and proceed.)

You can see the below code:

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
public void DoWork()

Explaining the concepts of WCF is beyond the scope of this tutorial. However, I just want to point out 2 things here:

[ServiceContract(Namespace = "")]
- This attribute above the class name indicates that this class is a WCF service.

- This attribute above the method name indicates that this method can be called from a WCF client.

Continue reading the next part of this chapter

Next Chapter: Calling WCF from Silverlight controls - Part II
Previous Chapter: How to pass parameters to Silverlight controls from ASP.NET pages ?
More Chapters: Silverlight Tutorials
More Tutorials: Tutorial Index

Top Contributors
TodayLast 7 Daysmore...

Awards & Gifts

Online Members

Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India