Silverlight and WCF

31 05 2008

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.

silverlight-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.

add-new-item

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.

wcf-service-added

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.

default-binding

Yes, you are right. WCF by default uses wsHttpBinding and thus we need to change the binding to basicHttpBinding. Below is the modified version:

modified-binding

How do we use this service in our Silverlight application? Below is a screenshot of our Silverlight application.

screenshot-control

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 :D

Right click on the Reference and select Add Service Reference

silverlight-add-service-reference

We will be presented with a dialog box to add a service reference.

service-discover

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.

calculator-service-discovered

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.

silverlight-added-wcf-reference-files

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:

output

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:





Resharper 4.0 – Lambda Support

26 05 2008

Resharper 4.0 beta was out few days back. I found an excellent feature which I think should be blogged :)

Here is small test code snippet which uses RhinoMocks unit testing framework

You can see that Resharper has issued a warning against

Call(delegate { mockSubject.Remove(subject); });

And the warning message is as follows,

You can see that it is intelligent enough to deduce that lambda expressions can be used in the place of anonymous delegates! 8)

And once you allow Resharper to convert to lambda, you get this:

How cool is that 8)





ReSharper 4.0 Beta is out now!

23 05 2008

Resharper 4.0 Beta which supports C# 3.0, LINQ and Visual Studio 2008 is ready for download now! This is THE best addin for Visual Studio and I would recommend it to everyone who uses Visual Studio 8)

Here is an article which compares Plain Visual Studio 2008 Vs Resharper 4.0 Visual Studio 2008

You can download the beta here

Enjoy! :)








Follow

Get every new post delivered to your Inbox.