Wahlin on .NET

Creating a Silverlight-Enabled WCF Service

As Dan shows, creating Silverlight-enabled services is as easy as picking the proper template in Visual Studio 2008 and adding a little code.

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 my previous article, I 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. Figure 1 shows the template.


[Click on image for larger view.]
Figure 1. Once the Silverlight tools are installed for Visual Studio you'll see the Silverlight-enabled WCF Service option.

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 in Listing 1:

[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]
}
Listing 1: The default class generated by the Silverlight-enabled WCF Service template.

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 2 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; }
}
Listing 2: Adding a WCF operation named GetCustomer that returns a Customer object.

Creating Silverlight-enabled services is as easy as picking the proper template in Visual Studio 2008 and adding a little code. In the next article, I'll walk through the process of consuming a WCF service in Silverlight 2.

About the Author

Dan Wahlin (Microsoft MVP for ASP.NET and XML Web Services) is the founder of The Wahlin Group which specializes in .NET and SharePoint onsite, online and video training and consulting solutions. Dan also founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, Silverlight and Web Services in Microsoft's .NET platform. He's also on the INETA Speaker's Bureau and speaks at conferences and user groups around the world. Dan has written several books on .NET including "Professional Silverlight 2 for ASP.NET Developers," "Professional ASP.NET 3.5 AJAX, ASP.NET 2.0 MVP Hacks and Tips," and "XML for ASP.NET Developers." Read Dan's blog here.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube