Silverlight WCF Hosting :: Creating a Silverlight – Enabled WCF Service

Silverlight is capable of consuming data from a variety of sources including REST APIs, ASMX services, Windows Communication Foundation (WCF) services and other standards-compliant services. In our previous article, we discussed how Silverlight’s WebClient class could be used to communicate with a REST API and retrieve data. The ability to access data from REST APIs is nice any time you’d like to create mash-up applications that aggregate and display data from a variety of other sites and services.

However, for many applications you’ll want to retrieve and display your own data. Several different options exist for this including ASMX services, ADO.NET Data Services and WCF services. Let’s take a look at how to create a Silverlight-enabled WCF service using Visual Studio 2008.

Creating a WCF Service

WCF provides several different ways for clients to bind to a service. You can use wsHttpBinding, netTcpBinding, basicHttpBinding and many others depending on the needs of client applications. In cases where Silverlight 2 clients will be calling WCF services, you’ll need to use basicHttpBinding since Silverlight 2 doesn’t support the encryption classes necessary to encrypt and decrypt SOAP messages using WS-Security standards. If data needs to be secured between the client and the server, SSL (https) can be used.

Although you can create a WCF service and then manually change the binding in the service’s configuration file, the easiest way to get started creating WCF services is to use the Silverlight-enabled WCF Service template in the Add New Item dialog of Visual Studio 2008. By using this template, all of the necessary configuration options will automatically be created to allow for a basicHttpBinding.


You can add the WCF service to the test Web project that’s added by default when you create a new Silverlight 2 project or to another Web project. Once it’s added, a service class will be created for you as shown below

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CustomerService
{
    [OperationContract]
    public void DoWork()
    {
        // Add your operation implementation here
        return;
    }

    // Add more operations here and mark them with [OperationContract]
}

The basicHttpBinding required by Silverlight 2 is automatically added to web.config so that you don’t have to manually change any of the configuration settings. (Note that Silverlight 3 now supports binary message encoding by default which provides even better performance.)

Once the WCF service class is added into your Web project, you can write methods to return the necessary objects from the service and decorate the methods with the WCF OperationContract attribute. This attribute marks them as service operations so that they can be consumed by a Silverlight client. Listing below shows an example of adding a service operation called GetCustomer that returns a Customer object. Although the code returns a Customer object directly, you could of course add the necessary code to call a database and return dynamic data:

[ServiceContract(Namespace = "http://www.TheWahlinGroup.com")]
[AspNetCompatibilityRequirements(RequirementsMode =
  AspNetCompatibilityRequirementsMode.Allowed)]
public class CustomerService
{
    [OperationContract]
    public Customer GetCustomer(int id)
    {
        return new Customer { FirstName = “John”, LastName = “Doe”, Zip = 85244 };
    }

    // Add more operations here and mark them with [OperationContract]
}

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Zip { get; set; }
} 

Comments are closed.

Sponsors

JaringanHosting.com

Cheap Reseller Hosting

Best Blog Hosting Provider

Best European ASP.NET 4.0 Hosting Provider