In the previous session we discussed ways to consume WCF service from a WPF client via REST. In this session we’re going to investigate ways to consume the same WCF service from a Silverlight client over SOAP.
- Create and publish a cross domain policy to allow Silverlight to consume WCF service
-
Sample crossdomain.xml file
<?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> -
Publish the file to the server root, e.g., C:\inetpub\wwwroot
-
- Create a Silverlight client project
-
For our example, we’ll modify the MainPage.xaml file of the Silverlight project by copying and tweaking the content from our previously created WPF client and it should appear as in the sample below
<UserControl xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="Demo.Wcf.SilverlightClient.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="145,12,0,0" Name="txtRegistrant" VerticalAlignment="Top" Width="120" />
<Button Height="23" HorizontalAlignment="Left" Margin="91,59,0,0" Name="btnRegisterWs" VerticalAlignment="Top" Width="75" Click="btnRegisterWs_Click" Content="Register(ws)"></Button>
<Button Height="23" HorizontalAlignment="Right" Margin="0,59,118,0" Name="btnRegisterRest" VerticalAlignment="Top" Width="75" Click="btnRegisterRest_Click" Content="Register(rest)"></Button>
<Button HorizontalAlignment="Left" Margin="10,59,0,0" Name="btnListWs" Width="75" Height="23" VerticalAlignment="Top" Click="btnListWs_Click" Content="List(ws)"></Button>
<Button HorizontalAlignment="Right" Margin="0,59,199,0" Name="btnListRest" Width="75" Height="23" VerticalAlignment="Top" Click="btnListRest_Click" Content="List(rest)"></Button>
<data:DataGrid AutoGenerateColumns="true" Margin="10,88,12,0" Name="dg"></data:DataGrid>
<Button Height="23" HorizontalAlignment="Left" Margin="172,59,0,0" Name="btnDeleteWs" VerticalAlignment="Top" Width="75" Click="btnDeleteWs_Click" Content="Delete(ws)"></Button>
<dataInput:Label Height="28" HorizontalAlignment="Left" Margin="10,10,0,0" Name="lblRegistrant" VerticalAlignment="Top" Width="120" Content="Registrant Name"></dataInput:Label>
<Button Height="23" HorizontalAlignment="Right" Margin="0,59,37,0" Name="btnDeleteRest" VerticalAlignment="Top" Width="75" Click="btnDeleteRest_Click" Content="Delete(rest)"></Button>
</Grid>
</UserControl> -
For now ignore the buttons named with trailing “Rest”, it’ll be the focus of our next session. The finished user interface should appear as below
-
- Add a service reference to the Silverlight project (not the Silverlight host site), pointing to the WCF host, which in our case is http://localhost/Demo.Wcf.Host/RegistrationService.svc
- Give the reference an appropriate name, e.g., RegSvcRef
-
Code the button click event handlers as in the sample below
private void btnListWs_Click(object sender, RoutedEventArgs e)
{
RegSvcRef.RegistrationClient svc = new RegSvcRef.RegistrationClient();
svc.GetRegistrationListCompleted += new EventHandler<GetRegistrationListCompletedEventArgs>(svc_GetRegistrationListCompleted);
svc.GetRegistrationListAsync();
}
private void btnRegisterWs_Click(object sender, RoutedEventArgs e)
{
Registration reg = new Registration();
reg.RegistrantName = txtRegistrant.Text;
RegSvcRef.RegistrationClient svc = new RegSvcRef.RegistrationClient();
svc.RegisterCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(svc_RegisterCompleted);
svc.RegisterAsync(reg);
}
private void svc_RegisterCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
RegSvcRef.RegistrationClient svc = new RegSvcRef.RegistrationClient();
svc.GetRegistrationListCompleted += new EventHandler<GetRegistrationListCompletedEventArgs>(svc_GetRegistrationListCompleted);
svc.GetRegistrationListAsync();
}
private void svc_GetRegistrationListCompleted(object sender, GetRegistrationListCompletedEventArgs e)
{
dg.ItemsSource = e.Result;
}
private void btnDeleteWs_Click(object sender, RoutedEventArgs e)
{
RegSvcRef.RegistrationClient svc = new RegSvcRef.RegistrationClient();
Registration reg = dg.SelectedItem as Registration;
if (reg != null)
{
svc.DeleteRegistrationCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(svc_DeleteRegistrationCompleted);
svc.DeleteRegistrationAsync(reg.Id);
}
}
private void svc_DeleteRegistrationCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
RegSvcRef.RegistrationClient svc = new RegSvcRef.RegistrationClient();
svc.GetRegistrationListCompleted += new EventHandler<GetRegistrationListCompletedEventArgs>(svc_GetRegistrationListCompleted);
svc.GetRegistrationListAsync();
}
The Silverlight client should now be operational and be able to consume the WCF service via SOAP. In the next session we’ll modify our Silverlight application to consume the WCF service via REST