With the release of Silverlight 2 beta 1, now developers can use .Net to create Silverlight applications To be more precise, developers can use XAML and VB.NET or C# as the back-end code language. To know more about Silverlight and what tools you need to start developing Silverlight applications, visit my post here.
This post focuses on how to make your Silverlight applications interact with Windows Communication Foundation(WCF) services. Visual Studio 2008 introduced several new WCF features. We will create a small calculator service which is going to add two numbers and return the result to the user.
First, let us create a Silverlight Web Application project.
Visual Studio creates the necessary files needed for your solution. Let us straight away start with adding our WCF service. Visual Studio creates two projects, one being the actual Silverlight control project and the other being the web application project where the Silverlight control is hosted. So, we would be able to add a WCF service to our web application project and then reference the service in our Silverlight control project. Right click the web application project and add a WCF service to the project.
Name the service as CalculatorService. Visual Studio adds the necessary files such as the SVC and code-behind files and also the service configuration to the web.config file.
I have modified the service to hold only function as shown below:
[ServiceContract]
public interface ICalculatorService
{
[OperationContract]
int AddTwoNumbers(int num1,int num2);
}
And here is the implementation:
#region ICalculatorService Members
public int AddTwoNumbers(int num1, int num2)
{
return (num1 + num2);
}
#endregion
Nothing complex, just simple addition and return the result 😉
Now comes the most important thing, at least for this beta 1 release – Silverlight supports only basicHttpBinding and thus our WCF service can only use basicHttpBinding if we are to interact with a Silverlight client. Let us check our CalcultorService’s binding in the web.config file under the system.servicemodel section.
Yes, you are right. WCF by default uses wsHttpBinding and thus we need to change the binding to basicHttpBinding. Below is the modified version:
How do we use this service in our Silverlight application? Below is a screenshot of our Silverlight application.
The interface is pretty simple accepting two numbers from the user and when Add is clicked, our service’s AddTwoNumbers is invoked to get the result.
How do we reference our WCF service now? We do the same way as we do in normal WPF or WinForms applications 😀
Right click on the Reference and select Add Service Reference
We will be presented with a dialog box to add a service reference.
We can use the Discover option to discover any WCF services in our solution. As our CalculatorService is hosted in the same solution, the Discover would be able to find our service.
Let us give a name to our service reference – CalculatorServiceProxy 🙂
Once we add our service, we can see that the Silverlight project now has the necessary files to interact with our WCF service.
Now we are ready to add our code to the button Click event handler. Below is the code snippet.
private void Button_Click(object sender, RoutedEventArgs e)
{
int num1 = Convert.ToInt32(TxtNum1.Text);
int num2 = Convert.ToInt32(TxtNum2.Text);
CalculatorServiceClient calculator_proxy =
new CalculatorServiceClient();
calculator_proxy.AddTwoNumbersCompleted +=
new EventHandler<AddTwoNumbersCompletedEventArgs>
(calculator_proxy_AddTwoNumbersCompleted);
calculator_proxy.AddTwoNumbersAsync(num1, num2);
}
void calculator_proxy_AddTwoNumbersCompleted(object sender,
AddTwoNumbersCompletedEventArgs e)
{
HtmlPage.Window.Alert(Convert.ToString(e.Result));
}
You guessed it right – WCF reference adds the asynchronous operations to our Silverlight project and it does makes more sense to use asynchronous operations in a Silverlight environment as we cant block the browser and make the user wait.
Let us build and execute our project 🙂
And here is the screenshot:
That’s it! Now we have a Silverlight application interacting with a WCF service 😉
Wasn’t it easy 8)
The sample can be downloaded here: