Compartir a través de


How to: Build a Service for Silverlight Clients

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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.

For a procedure that outlines how to access the service created in this topic from a Silverlight client, see How to: Access a Service from Silverlight.

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

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

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

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

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

    [OperationContract]
    public int CountUsers()
    {
        return 2;
    }
    [OperationContract]
    public User GetUser(int id)
    {
      if (id == 1)
      {
      return new User() { IsMember = true, Name = "Paul", Age = 24};
      }
      else
      {
      return new User() { IsMember = false, Name = "John", Age = 64};
      }
    }
    

    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.

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

    public class User
    {
    
        public bool IsMember { get; set; }
    
        public string Name { get; set; }
    
        public int Age { get; set; }
    }
    

    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.

    You do not need to apply the DataContractAttribute and DataMemberAttribute, to opt into the serialization process. For more information about the types that can be used in services, see Types Supported by the Data Contract Serializer.

  6. 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 https://localhost:1111, and your service might be hosted on https://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.

[OperationContract]
public IEnumerable<User> GetUsersByName(string nameToSearch)
{
    MyDatabaseEntities e = new MyDatabaseEntities();
    return (from user in e.User where user.Name.Contains(nameToSearch) select user);
}

Example

The following samples summarize the code in the CustomerService.svc.cs file after completing the preceding procedure.

//CustomerService.svc.cs
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;

namespace SilverlightApplication1.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CustomerService
    {
        [OperationContract]
        public int CountUsers()
        {
            return 2;
        }
        [OperationContract]
        public User GetUser(int id)
        {
            if (id == 1)
            {
                return new User() { IsMember = true, Name = "Paul", Age = 24 };
            }
            else
            {
                return new User() { IsMember = false, Name = "John", Age = 64 };
            }
        }
    }

// All public read/write properties and fields of a type are serialized by default.

    public class User
    {

        public bool IsMember { get; set; }


        public string Name { get; set; }


        public int Age { get; set; }
    }

}

See Also

Send comments about this topic to Microsoft.

Copyright © 2010 by Microsoft Corporation. All rights reserved.