In this article

04/12/2010

7 minutes to read

In this article

How to: Build a Service for Silverlight Clients

Silverlight applications frequently need to access some data or functionality on the back-end server. For example, if you are writing a customer management application, the server might contain a database of customers that your Silverlight application will need to retrieve. If you are writing a game, the server might contain high-score information that your Silverlight application will need to update. In many cases, the best way to make functionality on the server available to Silverlight is to create a Web service, as described in this topic.

This topic describes how to add a Silverlight-enabled service to an existing server-side project. Typically, this will be the default Web application that is included by Visual Studio 2010 in your Silverlight solution. For example, if you are building an application called SilverlightApplication1, you will see a Web application called SilverlightApplication1.Web in Solution Explorer. But you can add a service to any other Web site or Web application, or even create an entirely new WCF service application project.

This procedure assumes that you are using Visual Studio 2010 for your development environment. For convenience, the first few steps describe how to generate the default Web application when creating a Silverlight solution. If you are using another project to host the service, you can skip these steps.

To add a Silverlight-enabled WCF service to an existing Web site or Web application

From the File menu, select New, then Project, then Silverlight from the Project types, and then select Silverlight Application from the Visual Studio installed templates. Accept the default NameSilverlightApplicaton1 and click OK.

In the New Silverlight Application wizard that pops up, accept the Host the Silverlight application in a new Web site default that is selected. Accept the default value SilverlightApplication1.Web for the New Web project name and the default ASP.NET Web Application Project as the New Web project type. Then click OK.

To add a Silverlight-enabled Windows Communication Foundation (WCF) service to the default Web application that is generated, right-click the SilverlightApplication1.Web project in Solution Explorer and select Add, then New Item to open the Add New Item window. Select Silverlight from the relevant language group (Visual C# or Visual Basic) in the Installed Templates pane on the left, and then select the Silverlight-enabled WCF Service template from the central pane. Change the name of the service to CustomerService.svc in the Name box near the bottom, and then click the Add button to the right of this box.

To define and implement the CustomerService contract, replace the DoWork method in the CustomerService class (already defined in the CustomerService.svc.cs file) with the operations you want the service to support. In this example, we will add two operations that will enable the Silverlight client to retrieve the number of users in a database, and to retrieve each individual user’s information.

Do not forget to apply the OperationContractAttribute attribute to each operation. The service you are defining is based on the Windows Communication Foundation (WCF) technology. This topic uses only basic WCF features. For more information, refer to the WCF documentation, and specifically the topic Designing and Implementing Services.

Types that are used in services need to follow certain rules referred to as a data contract. The simplest way to create a valid type is to make sure that it is public, it has public members, and it has a public constructor that does not require any parameters. Making everything public implicitly defines the data contract for this type. For example, the User type in the GetUser operation can be implemented (within the SilverlightApplication1.Web namespace, by following the CustomerService class) as follows.

The DataContractSerializer serializes all publicly visible types by default, including all the public read/write properties and fields of these types. So the members of the User class will be serializable. For information about the default serialization behavior, see Serializable Types.

To test the service implementation in Visual Studio, select the CustomerService.svc file in Solution Explorer, and then right-click and select View in Browser (or press CTRL + F5) to display a test page for the service. You should see the CustomerService Service test page, which confirms the service is available.

Additional Considerations When Creating Services

There are certain restrictions on the types of services that Silverlight can access. If you are creating your WCF service without using the Silverlight-enabled WCF Service template, you must make sure that the WCF service is configured with the BasicHttpBinding for it to be supported. The PollingDuplexHttpBinding is also supported. For more information about using the duplex binding, see Building and Accessing Duplex Services. Note that other WCF bindings are not available because they require features that are not supported by the Silverlight client. WSHttpBinding, for example, requires sessions and transactions, which are not supported. For more information about Silverlight 4 restrictions when accessing SOAP services, including ones not created by using WCF or even the .NET Framework, see Accessing SOAP Services.

When creating a service anywhere other than on the Web site that hosts your Silverlight application, cross-domain issues can arise. Cross-domain calls between Silverlight applications and services present a security vulnerability and must be specifically enabled by an appropriate cross-domain policy. For procedures that describe how to implement such a policy, see Making a Service Available Across Domain Boundaries.

This situation frequently arises when debugging several separate Web application projects in Visual Studio, because a different port number is assigned to each Web application. For example, your Silverlight application might be hosted on http://localhost:1111, and your service might be hosted on http://localhost:2222. Even though the machine name is the same (localhost), the difference in port numbers is sufficient to create a cross-domain situation that you must deal with as described in Making a Service Available Across Domain Boundaries.

When you use the Silverlight-enabled WCF Service template, your Web.config file is automatically modified to turn on ASP.NET Compatibility Mode, to make it easier to integrate your service-backed Silverlight application with the ASP.NET security model. It would enable you, for example, to restrict access to certain services to authenticated users, or only to users in a certain Role. Any WCF services present in the same project must be configured to allow the ASP.NET compatibility mode by using the AspNetCompatibilityRequirementsAspNetCompatibilityRequirementsattribute. This is done automatically for any services added by using the Silverlight-enabled WCF Service template, but you must do this manually for any other WCF services you might have in the same project. An alternative to this is to turn off ASP.NET Compatibility Mode in Web.config if none of your services require it.

When using services to access databases, it is convenient to use types automatically generated from a database using the ADO.NET Entity Framework. These generated types are fully compatible with services intended for Silverlight use. It is also frequently convenient to use LINQ in service implementation. For example, assuming you have a database with a Users table and have generated a MyDatabaseEntities entity model from your database, an operation to find users by name could be implemented as follows.