Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

How to: Use a Windows Communication Foundation Client

This is the last of six tasks required to create a basic Windows Communication Foundation (WCF) application. For an overview of all six of the tasks, see the Getting Started Tutorial topic.

Once a Windows Communication Foundation (WCF) proxy has been created and configured, a client instance can be created and the client application can be compiled and used to communicate with the WCF service. This topic describes procedures for instantiating and using a WCF client. This procedure does three things:

  1. Instantiates a WCF client.

  2. Calls the service operations from the generated proxy.

  3. Closes the client once the operation call is completed.

To use a Windows Communication Foundation client

  • Open the Program.cs or Program.vb file from the GettingStartedClient project and replace the existing code with the following code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using GettingStartedClient.ServiceReference1;
    namespace GettingStartedClient
        class Program
            static void Main(string[] args)
                //Step 1: Create an instance of the WCF proxy.
                CalculatorClient client = new CalculatorClient();
                // Step 2: Call the service operations.
                // Call the Add service operation.
                double value1 = 100.00D;
                double value2 = 15.99D;
                double result = client.Add(value1, value2);
                Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
                // Call the Subtract service operation.
                value1 = 145.00D;
                value2 = 76.54D;
                result = client.Subtract(value1, value2);
                Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
                // Call the Multiply service operation.
                value1 = 9.00D;
                value2 = 81.25D;
                result = client.Multiply(value1, value2);
                Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
                // Call the Divide service operation.
                value1 = 22.00D;
                value2 = 7.00D;
                result = client.Divide(value1, value2);
                Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
                //Step 3: Closing the client gracefully closes the connection and cleans up resources.
    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.ServiceModel
    Imports GettingStartedClientVB2.ServiceReference1
    Module Module1
        Sub Main()
            ' Step 1: Create an instance of the WCF proxy
            Dim Client As New CalculatorClient()
            'Step 2: Call the service operations.
            'Call the Add service operation.
            Dim value1 As Double = 100D
            Dim value2 As Double = 15.99D
            Dim result As Double = Client.Add(value1, value2)
            Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result)
            'Call the Subtract service operation.
            value1 = 145D
            value2 = 76.54D
            result = Client.Subtract(value1, value2)
            Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result)
            'Call the Multiply service operation.
            value1 = 9D
            value2 = 81.25D
            result = Client.Multiply(value1, value2)
            Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result)
            'Call the Divide service operation.
            value1 = 22D
            value2 = 7D
            result = Client.Divide(value1, value2)
            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result)
            ' Step 3: Closing the client gracefully closes the connection and cleans up resources.
            Console.WriteLine("Press <ENTER> to terminate client.")
        End Sub
    End Module

    Notice the using or imports statement that imports the GettingStartedClient.ServiceReference1. This imports the code generated by Add Service Reference in Visual Studio. The code instantiates the WCF proxy and then calls each of the service operations exposed by the calculator service, closes the proxy and terminates.

You have now completed the tutorial. You defined a service contract, implemented the service contract, generated a WCF proxy, configured a WCF client application, and then used the proxy to call service operations. To test out the application first run GettingStartedHost to start the service and then run GettingStartedClient. The output from GettingStartedHost should look like this:

The service is ready.
Press <ENTER> to terminate service.

Received Add(100,15.99)
Return: 115.99
Received Subtract(145,76.54)
Return: 68.46
Received Multiply(9,81.25)
Return: 731.25
Received Divide(22,7)
Return: 3.14285714285714

The output from GettingStartedClient should look like this:

Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press <ENTER> to terminate client.
© 2015 Microsoft