You are on page 1of 7

1.

Open VS2008
2. Select new website and click WCF service
3.

4. Remove existing Service1 and Iservice class and interface.


5. click add new item and select “ Silverlight-enabled WCF Service”
6.

I created sample wcf service name as “EmpService”


using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Collections;
using System.Collections.ObjectModel;
using System.IO;
using System.ServiceModel.Web;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class EmpService
{
[OperationContract]
public string GetEmployeeName(string empName)
{
return "NEW Emp:" + empName;
}

[OperationContract]
public Employee GetEmployeeDetails(Employee employee)
{
return employee;
}

[DataContract]
public class Employee : INotifyPropertyChanged
{
private int _empNo = 0;
private string _empName = string.Empty;

void OnpropertyChanged(string propertyname)


{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new
PropertyChangedEventArgs(propertyname));
}
public event PropertyChangedEventHandler PropertyChanged;

[DataMember]
public int EmpNo
{
get
{
return (_empNo);
}
set
{
_empNo = value;
OnpropertyChanged("EmpNo");
}
}
[DataMember]
public string EmpName
{
get
{
return _empName;
}
set
{
_empName = value;
OnpropertyChanged("EmpName");
}
}
}

7. Build it now, proceed further if there is no errors.


8. after run this service u can able to see Output screen of service like below url
http://localhost:3049/GlobalServices/EmpService.svc
9. .if you call in silverlight like above URL it will through an Error because
Localhost having Port numbers like localhost:3049 . so for avoid Port numbers
we have to deploy WCF service in IIS like below
10. Publish your WCF service before deploy.
11. like right click on you project and click Publish website.

12. add new project on solution


13.

14. Select in Othe rproject types-> setup and deployment->webSetup Project


15.

16. after enter deploy project name you can see below screen
17.

18. on web application folder -> right click select add-> folder and give your
published website files
19.

20. now right click on Bin folder and select add-> files Seelct bin folder of your
published wesite.
21.

22. now build your deployment project . you can get setup.exe
23. after setup your EXE . you can call in silverlight like Add service reference
name as “EmpServiceReference” give URL of you service which you host in
IIS .
24. before using WCF serive you have to add Two XML files in
C:\Inetpub\wwwroot
25. I. First one is 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 path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>

II. Second xml file is 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="*"/>
<allow-http-request-headers-from domain="*" headers="SOAPAction" />

</cross-domain-policy>

26. Silverlight application Main.xaml for above service


<UserControl x:Class="EmpSilverlight.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">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="EmpNo" Grid.Row="0" Grid.Column="0"/>
<TextBox x:Name="txtEmpNo" Grid.Row="0" Grid.Column="1"
Text="{Binding EmpNo,Mode=TwoWay}" Width="60" />
<TextBlock Text="EmpName" Grid.Row="1" Grid.Column="0"/>
<TextBox x:Name="txtEmpName" Grid.Row="1" Grid.Column="1"
Text="{Binding EmpName,Mode=TwoWay}" Width="60"/>
<Button x:Name="btnSubmit" Content="Submit"
Click="btnSubmit_Click" Grid.Row="2" Grid.Column="0"/>
</Grid>
</UserControl>
27. main.xaml.cs

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;
using EmpSilverlight.EmpServiceReference;
namespace EmpSilverlight
{
public partial class MainPage : UserControl
{
Employee employee = new Employee();
public MainPage()
{
InitializeComponent();
this.DataContext = employee;
}

private void btnSubmit_Click(object sender, RoutedEventArgs e)


{
EmpServiceClient empServiceClient = new EmpServiceClient();
empServiceClient.GetEmployeeNameCompleted+=new
EventHandler<GetEmployeeNameCompletedEventArgs>(empServiceClient_GetEmpl
oyeeNameCompleted);
empServiceClient.GetEmployeeNameAsync(employee.EmpName);
empServiceClient.GetEmployeeDetailsCompleted+=new
EventHandler<GetEmployeeDetailsCompletedEventArgs>(empServiceClient_GetE
mployeeDetailsCompleted);
empServiceClient.GetEmployeeDetailsAsync(employee);
}
private void empServiceClient_GetEmployeeNameCompleted(object
sender, EmpServiceReference.GetEmployeeNameCompletedEventArgs e)
{
string empno= e.Result;
}
private void empServiceClient_GetEmployeeDetailsCompleted(object
sender, EmpServiceReference.GetEmployeeDetailsCompletedEventArgs e)
{
Employee employee1 = e.Result;
}
}
}

You might also like