Basic Data Contract

Download sample

This sample demonstrates how to implement a data contract. Data contracts allow you to pass structured data to and from services. This sample is based on the Getting Started Sample but uses complex numbers instead of simple numeric types.

In this sample, the service is hosted by Internet Information Services (IIS) and the client is a console application (.exe).


The setup procedure and build instructions for this sample are located at the end of this topic.

The service contract for this service uses complex numbers, as shown in the following sample code:

// Define a service contract.
public interface ICalculator
    ComplexNumber Add(ComplexNumber n1, ComplexNumber n2);
    ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2);
    ComplexNumber Multiply(ComplexNumber n1, ComplexNumber n2);
    ComplexNumber Divide(ComplexNumber n1, ComplexNumber n2);

The DataContractAttribute and DataMemberAttribute attributes have been applied to the definition of the ComplexNumber class to indicate which fields of the class can be passed over the wire between the client and the service, as shown in the following sample code:

public class ComplexNumber
    public double Real = 0.0D;
    public double Imaginary = 0.0D;

    public ComplexNumber(double real, double imaginary)
        this.Real = real;
        this.Imaginary = imaginary;

The service implementation calculates and returns the appropriate result, accepting and returning numbers of the ComplexNumber type:

// This is the service class that implements the service contract.
public class CalculatorService : ICalculator
    public ComplexNumber Add(ComplexNumber n1, ComplexNumber n2)
        return new ComplexNumber(n1.Real + n2.Real, n1.Imaginary +

    public ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2)
        return new ComplexNumber(n1.Real - n2.Real, n1.Imaginary - 
    public ComplexNumber Multiply(ComplexNumber n1, ComplexNumber n2)
        double real1 = n1.Real * n2.Real;
        double imaginary1 = n1.Real * n2.Imaginary;
        double imaginary2 = n2.Real * n1.Imaginary;
        double real2 = n1.Imaginary * n2.Imaginary * -1;
        return new ComplexNumber(real1 + real2, imaginary1 + 

    public ComplexNumber Divide(ComplexNumber n1, ComplexNumber n2)
         ComplexNumber conjugate = 
              new ComplexNumber(n2.Real, -1*n2.Imaginary);
         ComplexNumber numerator = Multiply(n1, conjugate);
         ComplexNumber denominator = Multiply(n2, conjugate);
         return new ComplexNumber(numerator.Real / denominator.Real,

The client implementation also uses complex numbers. Both the service contract and the data contract are defined in the source file generatedClient.cs, which is generated by the Service Metadata Utility Tool (Svcutil.exe) from service metadata:

// Create a client.
DataContractCalculatorClient client = new DataContractCalculatorClient();
// Call the Add service operation.
ComplexNumber value1 = new ComplexNumber(); 
value1.Real = 1; 
value1.Imaginary = 2;
ComplexNumber value2 = new ComplexNumber(); 
value2.Real = 3;
value2.Imaginary = 4;
ComplexNumber result = proxy.Add(value1, value2);
Console.WriteLine("Add({0} + {1}i, {2} + {3}i) = {4} + {5}i",
      value1.Real, value1.Imaginary, value2.Real, value2.Imaginary, 
      result.Real, result.Imaginary); 

When you run the sample, the requests and responses of the operation are displayed in the client console window. Press ENTER in the client window to shut down the client:

    Add(1 + 2i, 3 + 4i) = 4 + 6i
    Subtract(1 + 2i, 3 + 4i) = -2 + -2i
    Multiply(2 + 3i, 4 + 7i) = -13 + 26i
    Divide(3 + 7i, 5 + -2i) = 0.0344827586206897 + 41i

    Press <ENTER> to terminate client.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

Footer image

Send comments about this topic to Microsoft.
© Microsoft Corporation. All rights reserved.

Community Additions