Click here to Skip to main content
13,866,638 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


94 bookmarked
Posted 17 Jun 2009
Licenced CPOL

4 Simple Steps to Consume WCF Service using Silverlight

, 13 Aug 2010
Rate this:
Please Sign up or sign in to vote.
4 simple steps to consume WCF service using Silverlight

Update: SilverLight FAQ's - Part 3 link added to this article.

4 Simple Steps to Consume WCF Service using Silverlight



Video demonstration One Way, Two Way and One Time Bindings using Silver light



This article will talk about 4 simple steps which will assist you to consume WCF service in a Silverlight application. It also has a simple sample source code which demonstrates all the 4 steps practically.

I have collected around 400 FAQ questions and answers in WCF, WPF, WWF, SharePoint, design patterns, UML, etc. Feel free to download these FAQ PDFs from my site

Step 1: Create Your WCF Service

The first step is to create your WCF service. When we create a WCF service, by default it creates ‘GetData’ function which takes in an integer value and returns back a string saying “You entered 10” , in case you passed ‘10’ as value to the function. We will try to consume this service in Silverlight in the coming steps:

public class Service1 : IService1
    public string GetData(int value)
        return string.Format("You entered: {0}", value);

Step 2: Enable Cross Domain for Your WCF Service

For this example, our WCF service and the Silverlight web application will be hosted in different IIS websites. In other words, they will be hosted in different domains. When we talk about different website, in other words they are hosted in different domains. For instance, it’s possible that your Silverlight web application is hosted in one domain like  and your WCF service is hosted in different domain i.e.

The WCF service needs to enable cross domain facility so that other domains can consume the WCF service.

Figure: Cross domain

We need to create two XML files (clientaccesspolicy.xml and crossdomain.xml) in the root directory of the WCF service to enable cross domain functionality.
Below is the XML code snippet for clientaccesspolicy.xml:

<?xml version="1.0" encoding="utf-8" ?>
<allow-from http-request-headers="*">
<domain uri="*"/>
<resource include-subpaths="true" path="/"/>

Below is the XML code snippet for crossdomain.xml:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM 

<allow-http-request-headers-from domain="*" headers="*"/>

Step 3: Add the WCF Service Reference

Create a simple Silverlight application and add the service reference to your Silverlight project. In order to call the WCF service, we need to define event handlers.

To consume the WCF service is a three step procedure.

  • In the first step, refer to the name space.
  • In the second step, create the object of your WCF service.
  • In the final step, we need to create an event handler which will get the results sent by the WCF service.

One of the important points to note is that the function ‘GetData’ is called asynchronously.

Step 4: Call the Service

Finally compile the program and enjoy the output.

Other Silverlight FAQs

In case you are a complete fresher to Silverlight, then below are some Silverlight FAQs which can give you a quick start in this topic.

Silverlight FAQ Part 1 

This tutorial has 21 basic FAQs which will help you understand WPF, XAML, help you build your first Silverlight application and also explain the overall Silverlight architecture.

SilverLight FAQ Part 2 (Animations and Transformations) 

This tutorial has 10 FAQ questions which start with Silverlight animation fundamentals and then show a simple animated rectangle. The article then moves ahead and talks about 4 different ways of transforming the objects.

SilverLight FAQ Part 3  

This article discusses 12 FAQ’s which revolve around bindings, layouts, consuming WCF services and how to connect to database through Silverlight.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...


Comments and Discussions

GeneralMessage Closed Pin
14-May-13 22:52
memberLalit24rocks14-May-13 22:52 
GeneralEasiest way to create and consume WCF Services in Pin
Lalit24rocks14-May-13 22:51
memberLalit24rocks14-May-13 22:51 
QuestionMy vote of 5 Pin
Taner Riffat29-Apr-13 14:33
memberTaner Riffat29-Apr-13 14:33 
Question5+ Pin
karins20-Feb-13 0:39
memberkarins20-Feb-13 0:39 
GeneralMy vote of 4 Pin
yoke26-Sep-12 1:22
memberyoke26-Sep-12 1:22 
GeneralGreat simple article for Cross Domain access in SilverLight Pin
Anand 149141912-Sep-12 4:03
memberAnand 149141912-Sep-12 4:03 
GeneralMy vote of 5 Pin
BechirBenltaief24-Dec-11 3:46
memberBechirBenltaief24-Dec-11 3:46 
GeneralMy vote of 1 Pin
Josep Balague13-Sep-11 22:01
professionalJosep Balague13-Sep-11 22:01 
GeneralMy vote of 5 Pin
Rupesh Kumar Tiwari8-Sep-11 5:40
memberRupesh Kumar Tiwari8-Sep-11 5:40 
GeneralMy vote of 5 Pin
Vano Maisuradze25-May-11 20:55
memberVano Maisuradze25-May-11 20:55 
GeneralMy vote of 4 Pin
abhix6-Sep-10 20:53
memberabhix6-Sep-10 20:53 
GeneralMy vote of 3 Pin
Reishabh29-Jun-10 22:38
memberReishabh29-Jun-10 22:38 
Generali want create a sepertae web service and seperate web project Pin
santosh d7-Jan-10 0:50
membersantosh d7-Jan-10 0:50 
Generalsl when run in IE6 security error Pin
lemonheart27-Oct-09 21:57
memberlemonheart27-Oct-09 21:57 
GeneralSL-enabled WCF service Pin
Ravi Bhavnani18-Jun-09 3:31
professionalRavi Bhavnani18-Jun-09 3:31 
GeneralRe: SL-enabled WCF service Pin
Shivprasad koirala18-Jun-09 4:28
memberShivprasad koirala18-Jun-09 4:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190214.1 | Last Updated 14 Aug 2010
Article Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid