Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Posts Tagged ‘WCF Service’

Calling a WCF service from Javascript

Posted by Viral Sarvaiya on May 16, 2011


we know how to use WCF service in silverlight and that shows in my some previous posts

but i face another problem in it, it can possible to call WCF service from javascipt?

Answer is yes  and i find interesting post.

for more detail click here.

thanks and enjoy…


Posted in .Net, ASP.NET, RIA WCF, Silverlight, WCF Services | Tagged: , , , , , , , | Leave a Comment »

Use of WCF Service in Silverlight

Posted by Viral Sarvaiya on August 26, 2010

hello friends….

what is WCF? see the link –

Here i demonstrate the use of the WCF Service in silverlight.

what is WCF so refer this

please confirm that silverlight 3 or 4 and silverlight tool kit is installed in your PC.

Step 1: Create New Silverlight Project.

In Visual Studio2008 or 2010, file menu -> New -> project.

Select Silverlight application, give name of project as “SilverlightTesingService” and open the project.

then check the checkbox true of the host the silverlight application in web site.

Now you have 2 projects in your soluion.

Step 2 : Create Wcf Service

Right click to SilverlightTesingService.web project, select add -> new item

choose silverlight in left panel and choose Silverlight-enable WCF Service. then give name “TestService.svc”.

so Service has been added to project

in the TestService.svc.cs file write the following

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;

namespace SilverlightTesingService.Web
 [ServiceContract(Namespace = "")]
 interface IService1
 Users GetUsers(int id);

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
 public class TestService : IService1
 #region IService1 Members

 Users IService1.GetUsers(int id)
 if (id == 1)
 return new Users() { IsMember = true, Name = "Viral", age = 27 };
 else if (id == 2)
 return new Users() { IsMember = true, Name = "Malhar", age = 24 };
 return new Users() { IsMember = false, Name = "There is no user there", age = 0 };


 public class Users
 public bool IsMember { get; set; }
 public string Name { get; set; }
 public int age { get; set; }

Step 3 : clientaccesspolicy.xml File

Now for the allowing cross domain access we have to put the clientaccesspolicy.xml file to the SilverlightTesingService.web folder.

For more information of this file click to

clientaccesspolicy.xml as below

<?xml version="1.0" encoding="utf-8" ?>
 <allow-from http-request-headers="*">
 <domain uri="*"/>
 <resource path="/" include-subpaths="true"/>

Step 4: Web.config file change

Now we have to consider that web.config file have defined end point

put this in between  configuration tag

 <behavior name="SilverlightTesingService.Web.TestServiceBehavior">
 <serviceMetadata httpGetEnabled="true" />
 <serviceDebug includeExceptionDetailInFaults="false" />
 <binding name="customBinding0">
 <binaryMessageEncoding />
 <httpTransport />
 <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
 <service behaviorConfiguration="SilverlightTesingService.Web.TestServiceBehavior" name="SilverlightTesingService.Web.TestService">
 <endpoint address="" binding="basicHttpBinding" contract="SilverlightTesingService.Web.IService1" />
 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

Step 5 : Add Service to Silverlight Application

To add the service you require first build the SilverlightTesingService.web project.

Now in the SilverlightTesingService project

right click to project -> add the Service References

New dialog box open . click to discover button. and select the TestService.svc

and give name of the service and click ok.

Service Reference folder has been added and the service has been added to this folder.

Step 6: Use of the WCF Service

in the mainpage.xaml take 2 textbox and 1 button as follow

<UserControl x:Class="SilverlightTesingService.MainPage"
 xmlns:d="" xmlns:mc=""
 mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
 <Grid x:Name="LayoutRoot">
 <TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
 <TextBox Height="23" HorizontalAlignment="Left" Margin="12,86,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
 <Button Content="Button" Height="23" HorizontalAlignment="Left" Click="button1_Click" Margin="12,49,0,0" Name="button1" VerticalAlignment="Top" Width="75" />

now in mainpage.xaml.cs file make the object of the service client.

after that make the completed event when that service call that function will call

and last when this service call is written

see the mainpage.xaml.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightTesingService
 public partial class MainPage : UserControl
 TestService.Service1Client ObjClient; //make the object of the service
 public MainPage()
 ObjClient = new SilverlightTesingService.TestService.Service1Client();  //mamory allocation of the service object
 ObjClient.GetUsersCompleted += new EventHandler<SilverlightTesingService.TestService.GetUsersCompletedEventArgs>(ObjClient_GetUsersCompleted);  //make the completed event of the service

 void ObjClient_GetUsersCompleted(object sender, SilverlightTesingService.TestService.GetUsersCompletedEventArgs e)
 textBox2.Text = e.Result.Name.ToString();

 protected void button1_Click(object sender, EventArgs e)
 ObjClient.GetUsersAsync(Convert.ToInt32(textBox1.Text)); // calling the service's function with the parameter

Step 7 : Run the application

when you add integer 1 in the first textbox and click to button the service will call……

Enjoy the WCF service……

Posted in ASP.NET, RIA WCF, WCF Services | Tagged: , , , , , , , , | 4 Comments »

%d bloggers like this: