This documentation is archived and is not being maintained.

How to: Implement a Windows Communication Foundation Service Contract

This is the second of six tasks required to create a basic Windows Communication Foundation (WCF) service and a client that can call the service. For an overview of all six tasks, see the Getting Started Tutorial topic.

Creating a WCF service requires that you first create the contract, which is defined using an interface. For more information about creating the interface, see How to: Define a Windows Communication Foundation Service Contract. The next step, shown in this example, is to implement the interface. This involves creating a class called CalculatorService that implements the user-defined ICalculator interface. The code used for this task is provided in the example following the procedure.

To implement a WCF service contract

  1. Create a new class called CalculatorService in the same file where you defined the ICalculator interface. The CalculatorService implements the ICalculator interface.

    Public Class CalculatorService
        Implements ICalculator
    
    
  2. Implement each method defined in the ICalculator interface within the CalculatorService class.

    Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add
        Dim result As Double = n1 + n2
        ' Code added to write output to the console window.
        Console.WriteLine("Received Add({0},{1})", n1, n2)
        Console.WriteLine("Return: {0}", result)
        Return result
    End Function
    
    Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Subtract
        Dim result As Double = n1 - n2
        Console.WriteLine("Received Subtract({0},{1})", n1, n2)
        Console.WriteLine("Return: {0}", result)
        Return result
    
    End Function
    
    Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Multiply
        Dim result As Double = n1 * n2
        Console.WriteLine("Received Multiply({0},{1})", n1, n2)
        Console.WriteLine("Return: {0}", result)
        Return result
    
    End Function
    
    
    Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Divide
        Dim result As Double = n1 / n2
        Console.WriteLine("Received Divide({0},{1})", n1, n2)
        Console.WriteLine("Return: {0}", result)
        Return result
    
    End Function
    
    
    ms734686.note(en-us,VS.90).gifNote:
    The write output code has been added to make testing convenient.

Example

The following code example shows both the interface that defines the contract and the implementation of the interface.

Imports System
Imports System.ServiceModel


Namespace Microsoft.ServiceModel.Samples
    ' Define a service contract.
    <ServiceContract([Namespace] := "http://Microsoft.ServiceModel.Samples")>  _
    Public Interface ICalculator
        <OperationContract()>  _
        Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double 
        <OperationContract()>  _
        Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double 
        <OperationContract()>  _
        Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double 
        <OperationContract()>  _
        Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double 
    End Interface
    
    ' Step 1: Create service class that implements the service contract.
    Public Class CalculatorService
        Implements ICalculator
        
        ' Step 2: Implement functionality for the service operations.
        Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add
            Dim result As Double = n1 + n2
            ' Code added to write output to the console window.
            Console.WriteLine("Received Add({0},{1})", n1, n2)
            Console.WriteLine("Return: {0}", result)
            Return result
        End Function

        Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Subtract
            Dim result As Double = n1 - n2
            Console.WriteLine("Received Subtract({0},{1})", n1, n2)
            Console.WriteLine("Return: {0}", result)
            Return result
        
        End Function

        Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Multiply
            Dim result As Double = n1 * n2
            Console.WriteLine("Received Multiply({0},{1})", n1, n2)
            Console.WriteLine("Return: {0}", result)
            Return result
        
        End Function
        
        
        Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Divide
            Dim result As Double = n1 / n2
            Console.WriteLine("Received Divide({0},{1})", n1, n2)
            Console.WriteLine("Return: {0}", result)
            Return result
        
        End Function
    End Class
End Namespace

Now the service contract is created and implemented. Build the solution to ensure there are no compilation errors and then proceed to How to: Host and Run a Basic Windows Communication Foundation Service to run the service. For troubleshooting information, see Troubleshooting the Getting Started Tutorial.

Compiling the Code

If you are using a command-line compiler, you must reference the System.ServiceModel assembly.

See Also


© 2007 Microsoft Corporation. All rights reserved.
Last Published: 2010-03-21
Show: