How to: Implement a Windows Communication Foundation Service Contract

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.

    C#
    public class CalculatorService : ICalculator
  2. Implement each method defined in the ICalculator interface within the CalculatorService class.

     
    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;
    
    }
    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.

    C#
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

你可能感兴趣的:(windows)