Delen via


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
    
    public class CalculatorService : 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
    
    public double Add(double n1, double n2)
    {
        double result = n1 + n2;
        Console.WriteLine("Received Add({0},{1})", n1, n2);
        // Code added to write output to the console window.
        Console.WriteLine("Return: {0}", result);
        return result;
    }
    
    public double Subtract(double n1, double n2)
    {
        double result = n1 - n2;
        Console.WriteLine("Received Subtract({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        return result;
    }
    
    public double Multiply(double n1, double n2)
    {
        double result = n1 * n2;
        Console.WriteLine("Received Multiply({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        return result;
    }
    
    public double Divide(double n1, double n2)
    {
        double result = n1 / n2;
        Console.WriteLine("Received Divide({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        return result;
    }
    

    Note

    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
using System;
using System.ServiceModel;

namespace Microsoft.ServiceModel.Samples
{
    // Define a service contract.
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
    }

    // Step 1: Create service class that implements the service contract.
    public class CalculatorService : ICalculator
    {
         // Step 2: Implement functionality for the service operations.
        public double Add(double n1, double n2)
        {
            double result = n1 + n2;
            Console.WriteLine("Received Add({0},{1})", n1, n2);
            // Code added to write output to the console window.
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Subtract(double n1, double n2)
        {
            double result = n1 - n2;
            Console.WriteLine("Received Subtract({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Multiply(double n1, double n2)
        {
            double result = n1 * n2;
            Console.WriteLine("Received Multiply({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Divide(double n1, double n2)
        {
            double result = n1 / n2;
            Console.WriteLine("Received Divide({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }
    }
}

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

Other Resources

Getting Started Sample
Self-Host


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