Creating a WCF Service, Business Object and Data Submission with Silverlight 4

9 min read

Data applications

When building applications that utilize data, it is important to start with defining what data you are going to collect and how it will be stored once collected. In the last chapter, we created a Silverlight application to post a collection of ink strokes to the server. We are going to expand the inkPresenter control to allow a user to submit additional information.

Most developers would have had experience building business object layers, and with Silverlight we can still make use of these objects, either by using referenced class projects/libraries or by consuming WCF services and utilizing the associated data contracts.

Time for action – creating a business object

We’ll create a business object that can be used by both Silverlight and our ASP.NET application. To accomplish this, we’ll create the business object in our ASP.NET application, define it as a data contract, and expose it to Silverlight via our WCF service.

Start Visual Studio and open the CakeORamaData solution. When we created the solution, we originally created a Silverlight application and an ASP.NET web project.

  1. In the web project, add a reference to the System.Runtime.Serialization assembly.
  2. Microsoft Silverlight 4 Business Application Development: Beginner’s Guide

  3. Right-click on the web project and choose to add a new class. Name this class ServiceObjects and click OK.
  4. Microsoft Silverlight 4 Business Application Development: Beginner’s Guide

  5. In the ServiceObjects class file, replace the existing code with the following code:
  6. using System;
    using System.Runtime.Serialization;

    namespace CakeORamaData.Web
    [DataContract] public class CustomerCakeIdea
    [DataMember] public string CustomerName { get; set; }
    [DataMember] public string PhoneNumber { get; set; }
    [DataMember] public string Email { get; set; }
    [DataMember] public DateTime EventDate { get; set; }
    [DataMember] public StrokeInfo[] Strokes { get; set; }

    [DataContract] public class StrokeInfo
    [DataMember] public double Width { get; set; }
    [DataMember] public double Height { get; set; }
    [DataMember] public byte[] Color { get; set; }
    [DataMember] public byte[] OutlineColor { get; set; }
    [DataMember] public StylusPointInfo[] Points { get; set; }

    [DataContract] public class StylusPointInfo
    [DataMember] public double X { get; set; }
    [DataMember] public double Y { get; set; }

  7. What we are doing here is defining the data that we’ll be collecting from the customer.

What just happened?

We just added a business object that will be used by our WCF service and our Silverlight application. We added serialization attributes to our class, so that it can be serialized with WCF and consumed by Silverlight.

The [DataContract] and [DataMember] attributes are the serialization attributes that WCF will use when serializing our business object for transmission. WCF provides an opt-in model, meaning that types used with WCF must include these attributes in order to participate in serialization. The [DataContract] attribute is required, however if you wish to, you can use the [DataMember] attribute on any of the properties of the class.

By default, WCF will use the System.Runtime.Serialization.DataContractSerialzer to serialize the DataContract classes into XML. The .NET Framework also provides a NetDataContractSerializer which includes CLR information in the XML or the JsonDataContractSerializer that will convert the object into JavaScript Object Notation (JSON). The WebGet attribute provides an easy way to define which serializer is used.

For more information on these serializers and the WebGet attribute visit the following MSDN web sites:

Windows Communication Foundation (WCF)

Windows Communication Foundation (WCF) provides a simplified development experience for connected applications using the service oriented programming model. WCF builds upon and improves the web service model by providing flexible channels in which to connect and communicate with a web service. By utilizing these channels developers can expose their services to a wide variety of client applications such as Silverlight, Windows Presentation Foundation and Windows Forms.

Service oriented applications provide a scalable and reusable programming model, allowing applications to expose limited and controlled functionality to a variety of consuming clients such as web sites, enterprise applications, smart clients, and Silverlight applications.

When building WCF applications the service contract is typically defined by an interface decorated with attributes that declare the service and the operations. Using an interface allows the contract to be separated from the implementation and is the standard practice with WCF.

You can read more about Windows Communication Foundation on the MSDN website at:

Time for action – creating a Silverlight-enabled WCF service

Now that we have our business object, we need to define a WCF service that can accept the business object and save the data to an XML file.

  1. With the CakeORamaData solution open, right-click on the web project and choose to add a new folder, rename it to Services.
  2. Right-click on the web project again and choose to add a new item. Add a new WCF Service named CakeService.svc to the Services folder. This will create an interface and implementation files for our WCF service. Avoid adding the Silverlight-enabled WCF service, as this adds a service that goes against the standard design patterns used with WCF:
  3. Microsoft Silverlight 4 Business Application Development: Beginner’s Guide

    The standard design practice with WCF is to create an interface that defines the ServiceContract and OperationContracts of the service. The interface is then provided, a default implementation on the server. When the service is exposed through metadata, the interface will be used to define the operations of the service and generate the client classes. The Silverlight-enabled WCF service does not create an interface, just an implementation, it is there as a quick entry point into WCF for developers new to the technology.

  4. Replace the code in the ICakeService.cs file with the definition below. We are defining a contract with one operation that allows a client application to submit a CustomerCakeIdea instance:
  5. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;

    namespace CakeORamaData.Web.Services
    // NOTE: If you change the interface name "ICakeService" here,
    you must also update the reference to "ICakeService" in Web.config.
    [ServiceContract] public interface ICakeService
    [OperationContract] void SubmitCakeIdea(CustomerCakeIdea idea);

  6. The CakeService.svc.cs file will contain the implementation of our service interface. Add the following code to the body of the CakeService.svc.cs file to save the customer information to an XML file:
  7. using System;
    using System.ServiceModel.Activation;
    using System.Xml;

    namespace CakeORamaData.Web.Services
    // NOTE: If you change the class name "CakeService" here, you
    must also update the reference to "CakeService" in Web.config.
    [AspNetCompatibilityRequirements(RequirementsMode =
    AspNetCompatibilityRequirementsMode.Allowed)] public class CakeService : ICakeService
    public void SubmitCakeIdea(CustomerCakeIdea idea)
    if (idea == null) return;
    using (var writer = XmlWriter.Create(String.Format(@"C:
    ProjectsCakeORamaCustomerData{0}.xml", idea.CustomerName)))

    writer.WriteAttributeString("name", idea.CustomerName);
    writer.WriteAttributeString("phone", idea.PhoneNumber);
    writer.WriteAttributeString("email", idea.Email);

    // <eventDate></eventDate>

    // <strokes>

    if (idea.Strokes != null && idea.Strokes.Length > 0)
    foreach (var stroke in idea.Strokes)
    // <stroke>

    writer.WriteAttributeString("width", stroke.Width.
    writer.WriteAttributeString("height", stroke.Height.

    writer.WriteAttributeString("a", stroke.Color[0].
    writer.WriteAttributeString("r", stroke.Color[1].
    writer.WriteAttributeString("g", stroke.Color[2].
    writer.WriteAttributeString("b", stroke.Color[3].

    writer.WriteAttributeString("a", stroke.
    writer.WriteAttributeString("r", stroke.
    writer.WriteAttributeString("g", stroke.
    writer.WriteAttributeString("b", stroke.

    if (stroke.Points != null && stroke.Points.Length > 0)
    foreach (var point in stroke.Points)
    writer.WriteAttributeString("x", point.
    writer.WriteAttributeString("y", point.

    // </stroke>

    // </strokes>



    We added the AspNetCompatibilityRequirements attribute to our CakeService implementation. This attribute is required in order to use a WCF service from within ASP.NET.

  8. Open Windows Explorer and create the path C:ProjectsCakeORamaCustomerData on your hard drive to store the customer XML files.
  9. One thing to note is that you will need to grant write permission to this directory for the ASP.NET user account when in a production environment.

  10. When adding a WCF service through Visual Studio, binding information is added to the web.config file. The default binding for WCF is wsHttpBinding, which is not a valid binding for Silverlight. The valid bindings for Silverlight are basicHttpBinding, binaryHttpBinding (implemented with a customBinding), and netTcpBinding. We need to modify the web.config, so that Silverlight can consume the service. Open the web.config file and add this customBinding section to the <system.serviceModel> node:
  11. <bindings>
    <binding name="customBinding0">
    <binaryMessageEncoding />
    <extendedProtectionPolicy policyEnforcement="Never" />

  12. We’ll need to change the <service> node in the web.config to use our new customBinding, (we use the customBinding to implement binary HTTP which sends the information as a binary stream to the service), rather than the wsHttpbinding from:
  13. <service behaviorConfiguration="CakeORamaData.Web.Services.
    <endpoint address="" binding="wsHttpBinding"
    <dns value="localhost" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IM
    etadataExchange" />

    To the following:

    <service behaviorConfiguration="CakeORamaData.Web.Services.
    <endpoint address="" binding="customBinding" bindingConfiguratio
    contract="CakeORamaData.Web.Services.ICakeService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMeta
    dataExchange" />

  14. Set the start page to the CakeService.svc file, then build and run the solution. We will be presented with the following screen, which lets us know that the service and bindings are set up correctly:
  15. Microsoft Silverlight 4 Business Application Development: Beginner’s Guide

  16. Our next step is to add the service reference to Silverlight. On the Silverlight project, right-click on the References node and choose to Add a Service Reference:
  17. Microsoft Silverlight 4 Business Application Development: Beginner’s Guide

  18. On the dialog that opens, click the Discover button and choose the Services in Solution option. Visual Studio will search the current solution for any services:
  19. Microsoft Silverlight 4 Business Application Development: Beginner’s Guide

  20. Visual Studio will find our CakeService and all we have to do is change the Namespace to something that makes sense such as Services and click the OK button:
  21. Microsoft Silverlight 4 Business Application Development: Beginner’s Guide

  22. We can see that Visual Studio has added some additional references and files to our project. Developers used to WCF or Web Services will notice the assembly references and the Service References folder:
  23. Microsoft Silverlight 4 Business Application Development: Beginner’s Guide

  24. Silverlight creates a ServiceReferences.ClientConfig file that stores the configuration for the service bindings. If we open this file, we can take a look at the client side bindings to our WCF service. These bindings tell our Silverlight application how to connect to the WCF service and the URL where it is located:
  25. <configuration>
    <binding name="CustomBinding_ICakeService">
    <binaryMessageEncoding />
    maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
    <extendedProtectionPolicy policyEnforcemen
    t="Never" />
    <endpoint address="http://localhost:2268/Services/
    binding="customBinding" bindingConfiguration="Cust
    name="CustomBinding_ICakeService" />


Please enter your comment!
Please enter your name here