In this article, we are going to learn about how to work with Services in Silverlight.
Working with Services in Silverlight
As we know that Silverlight (being a sandbox technology) doesn’t support working directly with ADO.NET, it implies that you can’t use ADO.NET code like SqlConnection, SqlCommand or SqlDataAdapter to connect to the database and fetch records or manipulate records. So the only option left is to interact with the services (either web or wcf).
NOTE: In case you are creating services in different project than the Silverlight hosting project, you need to specify the client access policy (in the client/consumer project) and cross domain policy (in the server project).
This article is the continuation of my last article in Silverlight controls series, read last article here.
Get 500+ ASP.NET web development Tips & Tricks and ASP.NET Online training here.
Client access policy -
<?xml version="1.0" encoding="utf-8" ?>
<domain uri="*" />
<resource path="/" include-subpaths="true" />
Cross domain policy -
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<allow-http-request-headers-from domain="*" headers="SOAPAction,Content-Type"/>
<!--<allow-http-request-headers-from domain="*" headers="*" secure="true" />-->
How to create and consume WCF Service in Silverlight?
To create a WCF Service, right click the project (In my case I am creating in a separate project) and select WCF Service, enter name and click Add.
This will add
IDemoDatabaseWCFService.cs file. Add a new method called GetRecords into the Interface with OperationContract attribute (this attribute expose this method as service). Now write the implementation of this method as shown below in the DemoDatabaseWCFService.svc.cs.
public List<PersonWCF> GetRecords()
List<PersonWCF> list = new List<PersonWCF>();
using (SqlConnection conn = new SqlConnection(ConnStr))
using (SqlCommand cmd = new SqlCommand("SELECT * FROM PersonalDetail", conn))
using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
FirstName = reader.GetString(reader.GetOrdinal("FirstName")),
LastName = reader.GetString(reader.GetOrdinal("LastName")),
Age = reader.GetInt32(reader.GetOrdinal("Age")),
Active = reader.GetBoolean(reader.GetOrdinal("Active"))
Build the project and View the
DemoDatabaseWCFService.svc in browser.
Now add a Silverlight page in the Silverlight project and retain the code snippet as below.
<Button Content="Get Data from Service" Click="Button_Click" />
<sdk:DataGrid x:Name="DataGrid1" />
Add the reference of the WCF Service by right clicking the project and selecting “
Add Service Reference…”
Give proper namespace and click OK.
Now write below code snippet in the Code behind of the Silverlight page.
private void Button_Click(object sender, RoutedEventArgs e)
DemoServicesSoapClient client = new DemoServicesSoapClient();
client.GetRecordsCompleted += new EventHandler<GetRecordsCompletedEventArgs>(client_GetRecordsCompleted);
void client_GetRecordsCompleted(object sender, GetRecordsCompletedEventArgs e)
if (e.Error != null)
TextBlock1.Text = "Sorry, an error occured. " + e.Error.Message;
DataGrid1.ItemsSource = e.Result;
Before you do this, you need to add the namespace of the WCF Service namespace (in our case
Now run the Silverlight application.
Hope this article was useful. Thanks for reading and hoping that you liked it.
Keep an eye on forth coming articles on Silverlight. To read my series of articles,click here.