Search This Blog


Oct 16, 2010

How to call WCF in Silverlight

Sponsored Links:
As we know Silverlight application’s are always run in client browser,
We cannot have direct access of them if we want to do any database related
action, WCF solves this problem. We can directly call WCF Methods in 
Silverlight. To get this done you have to work on two applications
  • WCFService Lib – because Silverlight can not communicate directly
  • Silverlight Application

Open VS2010 Select WCF ServiceLib Project then 
In Your WCFServiceLib Project You have define the Data Contract and

Define Data Contract

public class myComboontracts
public string CreationDate;
public string Total;
public static List<myComboontracts > ConverttoList(DataSet dsService)
List<myComboontracts > SList = new List<myComboontracts >();
foreach (DataRow dr in dsService.Tables[0].Rows)
myComboontracts sData = new myComboontracts ();
sData.CreationDate = dr["CreationDate"].ToString();
sData.Total = dr["Total"].ToString();
return SList;
 Define Service Contract 
public interface IService
List<EDWContracts> GetDetails(string str);
type following code in Service1.svc public classService1: IService, IClientAccessPolicy    {        yourdBobjDB;        public List<myComboontracts > GetDetails()        {          objDB = new yourdB();
 DataSet oDs = objDB.ReturnDataset ( “Your Query” ); /*  ReturnDataset  is my function which return * Dataset using connection * here i am not showing normal methods * like how to connect with database * I am asuming you already have your * own dataccess layer */ return myComboontracts.ConverttoList(oDs); }
Deploy/Host this service either on IIS or Windows, Now Open 

Silverlight application
Structure of Silverlight application

  • Add Web Service Reference then open MainPage.xml file
  • Place Drop Down (I am using RadControl’s Combo) , make sure it’s should not content any thing from client side (xaml) please remove itemcontent property from xamal view
  • Now goto Mainpage.xaml.cs and add create the object of your webserice
    ServiceReference1.ServiceClient s;
  • Declare the event Handler 
public MainPage()
{s = new ServiceReference1.ServiceClient();
s.GetMissingDealAssetWidgetCompleted += new 
  • Define the definition of Event handler
private void LoadDetails()
s = new ServiceReference1.ServiceClient();
s.GetDetailsCompleted += 
         new EventHandler<ServiceReference1.GetDetails>
That’s It

Follow by Email