JG Vimalan's Blog

It is all about coding!

Consuming WCF service in Silverlight application

In order to consume WCF service in your silverlight application, add the WCF Service as a service reference in your Silverlight application. Once you do that, you can see a ServiceReferences.ClientConfig file in your silverlight project. It will have the binding information and the endpoint as shown below,

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name=”basicEndPoint” maxBufferSize=”2147483647″ maxReceivedMessageSize=”2147483647″>
                    <security mode=”None” />
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address=”http://mySystem/WCFService/Manager.svc” binding=”basicHttpBinding”
                bindingConfiguration=”basicEndPoint” contract=”SampleWCFService.IManager”
                name=”basicEndPoint” />
        </client>
    </system.serviceModel>
</configuration>

So, its time to invoke the methods exposed by the WCF service. In silverlight, it is slightly different when compared to asp.net web application. You will call the asynchronous method and handle its completed event, where you get the result.

private void button1_Click(object sender, RoutedEventArgs e)
        {
            SampleWCFService.ManagerClient client = new
                SampleWCFService.ManagerClient();

            client.GetEmployeesCompleted += new
                EventHandler<SampleWCFService.GetEmployeesCompletedEventArgs>(client_GetEmployeesCompleted);

            client.GetEmployeesAsync(); //invoke the GetEmployees method here
        }

        void client_GetEmployeesCompleted(object sender, SampleWCFService.GetEmployeesCompletedEventArgs e)
        {
            //you will get the result object here
            System.Collections.ObjectModel.ObservableCollection<SampleWCFService.Employee> employees =
                e.Result;

            if (employees.Count() > 0)
            {
                //put code here
            }
        }

Advertisements

October 21, 2010 - Posted by | Silverlight, WCF

1 Comment »

  1. great blog

    Comment by bradshaw | December 2, 2010 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: