4 Simple steps to consume WCF service using Silverlight

Questpond
Posted by in Silverlight category on for Advance level | Views : 8901 red flag

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


 Download source code for 4 Simple steps to consume WCF service using Silverlight

4 Simple steps to consume WCF service using Silverlight
 

Introduction

Other silverlight FAQ

Step1:- Create your WCF Service

Step 2 :- Enable cross domain for your WCF service

Step 3 :- Add the WCF service reference and call the service

Step 4:- Add the WCF service reference and call the service

Source code
 

Introduction
 

This article will talk about 4 simple steps which will assist you to consume WCF service in a Silverlight application. It also had 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 PDF’s from my site http://www.questpond.com
 

Other silverlight FAQ
 

In case you are a complete fresher to silverlight then below are some silverlight FAQ’s which can give you a quick start in this topic.

Silverlight FAQ Part 1:- http://www.dotnetfunda.com/articles/article293.aspx  This tutorial has 21 basic FAQ’s which will help you understand WPF , XAML , help your build your first silverlight application and also explains the overall silverlight architecture.

SilverLight FAQ Part 2 (Animations and Transformations):- http://www.dotnetfunda.com/articles/article394-silverlight-faq-part-2-animations-and-transformations.aspx  This tutorial has 10 FAQ questions which starts with silverlight animation fundamentals and then shows a simple animated rectangle. The article then moves ahead and talks about 4 different ways of transforming the objects.
 

Step1:- 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 a integer value and returns back a string saying “You entered 10” , in case you have 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 silver light web application will be hosted in different IIS website. 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 silver light 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 and call the service
 

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 the name space. In second step1 create the object of your WCF service. In the final step we need to create a 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:- Add the WCF service reference and call the service
 

Finally compile the program and enjoy the output.
 

Source code
 

You can download the source code from top of this Articles
 

Page copy protected against web site content infringement by Copyscape

Login to vote for this post.

Comments or Responses

Posted by: Parasu on: 6/17/2009
Good job.

Login to post response

Comment using Facebook(Author doesn't get notification)