Click here to Skip to main content
6,630,289 members and growing! (22,260 online)
Email Password   helpLost your password?
Web Development » Silverlight » General     Intermediate License: The Code Project Open License (CPOL)

4 Simple Steps to Consume WCF Service using Silverlight

By Shivprasad koirala

4 simple steps to consume WCF service using Silverlight
WCF, Silverlight, Architect
Version:5 (See All)
Posted:17 Jun 2009
Updated:8 Oct 2009
Views:7,785
Bookmarked:17 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
6 votes for this article.
Popularity: 3.66 Rating: 4.70 out of 5

1

2

3
2 votes, 33.3%
4
4 votes, 66.7%
5

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

Table of Contents

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


Member
He thinks he was born for only one mission and thats technology.Keeping this mission in mind he established www.questpond.com where he has uploaded 500 videos on WCF,WPF,WWF,Silverlight,Design pattern, FPA , UML , Projects etc. He is also actively involved in RFC which is a financial open source made in C#. It has modules like accounting , invoicing , purchase , stocks etc.
Occupation: Architect
Company: http://www.questpond.com
Location: India India

Other popular Silverlight articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
Generalsl when run in IE6 security error Pinmemberlemonheart21:57 27 Oct '09  
GeneralSL-enabled WCF service PinmemberRavi Bhavnani3:31 18 Jun '09  
GeneralRe: SL-enabled WCF service PinmemberShivprasad koirala4:28 18 Jun '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Oct 2009
Editor: Deeksha Shenoy
Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project