Vous êtes sur la page 1sur 5

Get QuestPond’s DVD at the following book shops:

Computer Book Shop, Mumbai, Fort. Ph No: +91‐22‐22070989/6356, 66317922,


66317923/24. E‐mail: ‐ 
cbs@vsnl.com

Dharma Enterprises, Mumbai, Fort. Ph No: +91‐22‐22611760, 65718637, 64512196.


E‐mail: ‐
info@booksandlotsmore.com dharma@booksandlotsmore.com

GreenLeaf Book Shop, Mumbai. Ph No: +91‐22‐28770081, 28786269 Email: ‐


grenleaf@vsnl.com

Varsha Book Depot, Mumbai, Vashi. Ph No: +91‐22‐27896049, 27897079, 27894475.


Email: ‐
varshabook@yahoo.com, varshabook@gmail.com

Distributor in Hyderabad, Ph No: +91‐40‐23080615, 09391342107, 09866068037. Email:



uk_karansingh@yahoo.com

Rajkamal Book Shop, Hyderabad Ph No: +91‐040‐24756064/


27802113/32453620/24754671/24756064.
Rajlaxmi Book House, Hyderabad Ph No: +91‐040‐66789218.

DVD Title available: ‐


.NET Video Pack @ 1700 INR all videos from QuestPond including one month subscription
program fordetails of the contents log‐on to www.questpond.com

.NET Projects DVD @ 600 INR includes Invoicing Application end to end project using SDLC
cycle for details of the contents log‐on to www.questpond.com

Sharepoint, WCF, WPF, WWF, LINQ & SharePoint Videos @ 1000 INR for details of the
contents log‐on to www.questpond.com 

4 Simple Steps to Consume WCF Service using Silverlight

• Introduction
• Step 1: Create Your WCF Service
• Step 2: Enable Cross Domain for Your WCF Service
• Step 3: Add the WCF Service Reference
• Step 4: Call the Service

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

Vous aimerez peut-être aussi