hello friends….
what is WCF? see the link – https://viralsarvaiya.wordpress.com/2009/11/18/windows-communication-foundation/
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 { [OperationContract] 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 }; else return new Users() { IsMember = false, Name = "There is no user there", age = 0 }; } #endregion } 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 http://msdn.microsoft.com/en-us/library/cc197955%28VS.95%29.aspx
clientaccesspolicy.xml as below
<?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 path="/" include-subpaths="true"/> </grant-to> </policy> </cross-domain-access> </access-policy>
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
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="SilverlightTesingService.Web.TestServiceBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <bindings> <customBinding> <binding name="customBinding0"> <binaryMessageEncoding /> <httpTransport /> </binding> </customBinding> </bindings> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> <services> <service behaviorConfiguration="SilverlightTesingService.Web.TestServiceBehavior" name="SilverlightTesingService.Web.TestService"> <endpoint address="" binding="basicHttpBinding" contract="SilverlightTesingService.Web.IService1" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> </system.serviceModel>
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="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 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" /> </Grid> </UserControl>
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() { InitializeComponent(); 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……