How to consume/connect/use WCF service for Silverlight4 application.

The process is simple. You should know two things,what has to be provided as service.
Where it would be consumed.
Here Windows Communication Foundation lets you create services.
Silverlight application is the client which will consume it.

Steps for creating WCF service:
1) Select a Silverlight application hosted in Web application project.
2) Add to it a WCF service.

Img1
3) Three files will be added to your project.I have chosen the default names.

img2
4) The IService1.cs interface will define the contract and Service1.svc.cs will implement it.

Here MyHello is a simple method to return a message on input of a string.

Code in IService1.cs

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

namespace Sample1.Web
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string MyHello(string name);
    }
}

Code in Service1.svc.cs

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

namespace Sample1.Web
{
    public class Service1 : IService1
    {
        public string MyHello(string name)
        {
            return "Hello "+name+" from WCF service";
        }
    }
}

 
5) Once the service is created build the project and the view the .svc file in browser.

img3

 

Steps for consuming the service.
6) In our Silverlight application,in our xaml page we will add a textbox to enter a name , a button to call WCF service and a textblock to display the output.

img6  

<UserControl x:Class="Sample1.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:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Click to call WCF Service" Height="52" HorizontalAlignment="Left"   Margin="44,148,0,0" Name="btn_CallService" VerticalAlignment="Top" Width="320" Click="btn_CallService_Click" />
        <TextBox Height="64" HorizontalAlignment="Left" Margin="44,60,0,0" Name="txt_EnterName" VerticalAlignment="Top" Width="319" />
        <TextBlock Height="52" HorizontalAlignment="Left" Margin="44,222,0,0" Name="txt_DisplayHello" Text="" VerticalAlignment="Top" Width="318" />
    </Grid>
</UserControl>

 
7) Your silverlight application  has to first discover the service created. So add a service reference to your silverlight application project.

 img4

The project structure.

img5
8) Finally the last step. Create a proxy which is an object of ServiceClient to access the method. The methods are loaded asynchronously hence you may not see the name of the method,but two additional methods,one to load method asynchronously and the other to do something once its completed.

 

Code in MainPage.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.ServiceModel;

namespace Sample1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void btn_CallService_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();

           //Hooking up the event to display the entered text.
            proxy.MyHelloCompleted +=new EventHandler<ServiceReference1.MyHelloCompletedEventArgs>    (proxy_MyHelloCompleted);

           //Passing the text input
            proxy.MyHelloAsync(txt_EnterName.Text);

        }

     

        void proxy_MyHelloCompleted(object sender, ServiceReference1.MyHelloCompletedEventArgs e)
        {
            txt_DisplayHello.Text = e.Result;
        }

    }
}

9) Its done. Now when you run your application,on click event of button the WCF service will call the function asynchronously to take in the entered text and display the output in textblock.

img7

 

Note: In Silverlight 4 application when using a WCF service I did not have to change the bindings from wsHttpBinding to basicHttpBinding.

Else we had to do it in  earlier versions of Silverlight applications.

Follow

Get every new post delivered to your Inbox.

Join 41 other followers