Tutorials » Silverlight tutorials »

Calling WCF from Silverlight controls - Part II


This chapter is a continuation of the previous chapter and explains how to call WCF methods from the Silverlight controls.


This chapter is the continuation of the previous chapter. If you havent read the previous chapter, please read it before you continue here.

Let us add a new method to our WCF service class, decorated with the [OperationContract] attribute. Add a method as shown below:

[OperationContract]
public string GetName()
{
return "John";
}


As you can see, it is a simple method which returns a hard coded name.

Open your solution explorer. Right click on the file "Service1.svc" and select "Set as Startup Page".

Now run the application by pressing Ctrl + F5 (or, using the menu "Debug" > "Start Without Debugging")

The browser will be opened and Service1 meta data will be displayed. Ignore the content in the page, but copy the URL from the browser. The URL will look something like this:
http://localhost:1873/Service1.svc
The port number may be different in your case, but that is OK.

Return to your Solution Explorer in Visual Studio. Right click on your Silverlight project (not web project) and select "Add Service Reference".

In the field for Address, specify the URL you copied from the browser and press "GO".

In the bottom left, change the namespace to "MySampleService".

You should see a screen like this:



Press "OK" to add the service reference.

Now you can call our WCF service from the Silverlight client.

To test this, follow these steps:

1. Open the Page1.xaml file and add a textblock to display the string returned from the WCF method call.

2. Add an event handler for the "Loaded" event of the UserControl.

Complete code from Page1.xaml is given below:

<UserControl x:Class="Silverlight_With_WCF.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300"
Loaded="UserControl_Loaded">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock x:Name="textblock1" Width="200" Height="30"></TextBlock>
</Grid>
</UserControl>


Now go to the code behind file (Page1.xaml.cs) and add the WCF service call as shown below:


private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
MySampleService.Service1Client client = new MySampleService.Service1Client();
client.GetNameCompleted += new EventHandler(client_GetNameCompleted);
client.GetNameAsync();
}
void client_GetNameCompleted(object sender, MySampleService.GetNameCompletedEventArgs e)
{
textblock1.Text = (string)e.Result;
}


Open the web project in Solution Explorer and set the Silverlight test page as the startup page. Now run the project. You can see that the WCF service is called from Silverlight client and the result is displayed in the browser. (In our sample, the result from the WCF call is the name "John" and this name will be displayed in the screen).

You can download a sample project for this tutorial.



Next Chapter: Access Session variables from Silverlight controls
Previous Chapter: How to call WCF methods from Silverlight controls ?
More Chapters: Silverlight Tutorials
More Tutorials: Tutorial Index



Top Contributors
Today
    Last 7 Daysmore...

    Awards & Gifts

    Online Members

    More...
     
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India