Click here to Skip to main content
Click here to Skip to main content

4 Simple Steps to Consume WCF Service using Silverlight

By , 13 Aug 2010
 

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

 

Introduction

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 http://www.questpond.com.

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 www.xyz.com  and your WCF service is hosted in different domain i.e. www.pqr.com.

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" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource include-subpaths="true" path="/"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>

Below is the XML code snippet for crossdomain.xml:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM 
	"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>

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.

License

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

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India
Member

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMessage Automatically RemovedmemberLalit24rocks14 May '13 - 21:52 
Message Automatically Removed
GeneralEasiest way to create and consume WCF Services in asp.netmemberLalit24rocks14 May '13 - 21:51 
Easiest way to create and consume WCF Services in asp.net
http://www.webcodeexpert.com/2013/04/how-to-create-and-consume-wcf-services.html
QuestionMy vote of 5memberTaner Riffat29 Apr '13 - 13:33 
Brilliant Shiv.
 
Accurate, to the point, and most importantly, it works!
 
Well done.
 
One improvement would be to explain the "cross-domain-policy" which is set to the macromedia website.
 
What if you are running on domains that do not have internet access? Is there an internal policy you could reference instead. If so what would the url look like? e.g
 
http://somedomain.somewhere.local/xml/dtds/cross-domain-policy.dtd
 
Is there a default internal domain we could use for the policy reference?

Regards
Taner

Question5+memberkarins19 Feb '13 - 23:39 
Many thanks this is brilliant
GeneralMy vote of 4memberyoke26 Sep '12 - 0:22 
good stuff. just need to update some info for VS2010.
GeneralGreat simple article for Cross Domain access in SilverLightmemberMember 942469912 Sep '12 - 3:03 
Thanks shivprasad - for providing simple solutions for a great start. Please keep on helping .NET developers....AnandKumar
GeneralMy vote of 5memberBechirBenltaief24 Dec '11 - 2:46 
greate and simple !!
That we found in the real life of developper
GeneralMy vote of 1memberJosep Maria Roy13 Sep '11 - 21:01 
zip code not included
GeneralMy vote of 5memberRupeshKumar8 Sep '11 - 4:40 
good fundamental article
GeneralMy vote of 5memberVano Maisuradze25 May '11 - 19:55 
Thanks for this article. you saved my days.
GeneralMy vote of 4memberabhix6 Sep '10 - 19:53 
Siva, that was yet another excellent article from u; however u should have elaborated it bit more regarding WCF: hossting, why async, bindings etc!
 
& hey that new profile pic is KoOl. Is that Goa or what?? Smile | :)
GeneralMy vote of 3memberReishabh29 Jun '10 - 21:38 
Can you guide me regarding this in silver light 4
Generali want create a sepertae web service and seperate web projectmembersantosh d6 Jan '10 - 23:50 
Hi,
 
i read and see u r article it is good . now i want crate a separate Web Service ie(name as "WCFService" in IS) and seperate project ie (name as "MyFisrtSilverLightWebService"). i had done all this i get error of cross domain ...
and i want to use the same Web Service ie(name as "WCFService" in IS) in more then 1 project and in different - different computer
 
What will be the solution of this!
Generalsl when run in IE6 security errormemberlemonheart27 Oct '09 - 20:57 
hello ,I have a silverlight system ,use sl-enable wcf ,when i run in ie6 ,there has a security error,but it can run normal in ie7.What can I do? thanks .
GeneralSL-enabled WCF servicememberRavi Bhavnani18 Jun '09 - 2:31 
Thanks for your article.
 
I think it's important to point out that the WCF service needs to be a SL-enabled service - i.e. configured with BasicHttpBinding. This is required for it to be consumable by an SL client. Imho the easiest way to do this is to select the "Silverlight-enabled WCF Service" template in VS.
 
/ravi
 
My new year resolution: 2048 x 1536
Home | Articles | My .NET bits | Freeware
ravib(at)ravib(dot)com

GeneralRe: SL-enabled WCF servicememberShivprasad koirala18 Jun '09 - 3:28 
Yes i have missed that step. I will highlight it. Thanks for your inputs
 
Footprints on the sand are not made by sitting at the shore.

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

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