Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 How to: Use a Client
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
How to: Use a Windows Communication Foundation Client

This is the sixth 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 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 creating and using a WCF client. This procedure does three things: creates a WCF client, calls the service operations from the generated proxy, and closes the client once the operation call is completed.

The code discussed in the procedure is also provided in the example following the procedure. The code in this task should be placed in the Main() method of the generated Program class in the client project.

To use a Windows Communication Foundation client

  1. Create an EndpointAddress instance for the base address of the service you are going to call and then create an WCF Client object.

    Visual Basic
    ' Step 1: Create an endpoint address and an instance of the WCF Client.
    Dim epAddress As New EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService")
    Dim Client As New CalculatorClient(New WSHttpBinding(), epAddress)
    C#
    //Step 1: Create an endpoint address and an instance of the WCF Client.
    CalculatorClient client = new CalculatorClient();
  2. Call the client operations from within the Client.

    Visual Basic
    '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)
    C#
    // 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);
  3. Call Close on the WCF client and wait until the user presses the enter key to terminate the application.

    Visual Basic
    ' Step 3: Closing the client gracefully closes the connection and cleans up resources.
    Client.Close()
    
    Console.WriteLine()
    Console.WriteLine("Press <ENTER> to terminate client.")
    Console.ReadLine()
    C#
    //Step 3: Closing the client gracefully closes the connection and cleans up resources.
    client.Close();
    
    
    Console.WriteLine();
    Console.WriteLine("Press <ENTER> to terminate client.");
    Console.ReadLine();

Example

The following example shows you how to create a WCF client, how to call the operations of the client, and how to close the client once the operation call is completed.

Compile the generated WCF client and the following code example into an executable named Client.exe. Be sure to reference System.ServiceModel when compiling the code.

Visual Basic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.ServiceModel


Module Client

    Sub Main()
        ' Step 1: Create an endpoint address and an instance of the WCF Client.
        Dim epAddress As New EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService")
        Dim Client As New CalculatorClient(New WSHttpBinding(), epAddress)

        '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.
        Client.Close()

        Console.WriteLine()
        Console.WriteLine("Press <ENTER> to terminate client.")
        Console.ReadLine()

    End Sub
End Module
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace ServiceModelSamples
{

    class Client
    {
        static void Main()
        {
            //Step 1: Create an endpoint address and an instance of the WCF Client.
            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.
            client.Close();
            

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();

        }
    }
}

Ensure the service is running before attempting to use the client. For more information, see How to: Host and Run a Basic Windows Communication Foundation Service.

To launch the client, start a Windows SDK console session by selecting CMD Shell under the Microsoft Windows SDK entry in the Start menu. Navigate to the C:\Users\<user name>\Documents\Visual Studio 2005\Projects\Service\Client\bin\Debug directory, and type client and press ENTER. The operation requests and responses appear in the client console window as follows.

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.

If you see this output, you have successfully completed the tutorial. This sample demonstrates how to configure the WCF client in code. For troubleshooting information, see Troubleshooting the Getting Started Tutorial.

See Also


© 2007 Microsoft Corporation. All rights reserved.
Build Date: 2009-10-13
Community Content   What is Community Content?
Add new content RSS  Annotations
Generating the Proxy client      stevegw   |   Edit   |   Show History

Currently there is a bug with vs2008 Beta 2 so when generating the Proxy client

open a Visual Studio Command Prompt. At the command prompt

sn -Vr "c:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\SvcUtil.exe"
svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/ServiceModelSamples/service

Tags What's this?: bug (x) Add a tag
Flag as ContentBug
The same bug as elsewhere.... need consistency checking...      Jeff Certain   |   Edit   |   Show History
This line
Dim epAddress AsNew EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService")
should be:

Dim epAddress AsNew EndpointAddress("http://localhost:8000/ServiceModelSamples/Services/CalculatorService")
This problem is throughout the code, and stems from the error in the first step, which you fixed for C# only when it was reported initially.
Do we need to explicitly call client.open to access service endpoint      henryz   |   Edit   |   Show History
the code example fails the first time due to 'service is listening at endpoint "http://localhost:8000/ServiceModelSamples/Service/CalculatorSerivce"

i added client.Open() in the client code and that seems to fix the problem.

however, this cannot be reproduced. after the first failure, subsequent calls to the service WITHOUT explicitly calling client.Open() also succeeds.

could someone explain to me why? thanks.
Tags What's this?: Add a tag
Flag as ContentBug
Works Great for me      ausi1 ... Thomas Lee   |   Edit   |   Show History
Thanks the VB example works great for me
Worked perfectly...      Janus007   |   Edit   |   Show History
The same bug as elsewhere.... need consistency checking... -> Yes mister.. the bug is in your spelling of the Service class :)
Tags What's this?: Add a tag
Flag as ContentBug
C# example code missing endpoint      Robert Hodgman ... jadams   |   Edit   |   Show History
I had to add a line in Step 1 C# code to make the example work:

EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Services/CalculatorService");

The VB has it and the C# comment for Step 1 indicates we should create the EndpointAddress.

This is not necessary if the tool generated the proxy and app.config.
If you are getting "An error occurred when verifying security for the message" error....      WickedSmaht Solutions   |   Edit   |   Show History
If you are getting "An error occurred when verifying security for the message" error when testing this on two machines on your home network, make sure the clock time (as in 8:02pm, etc) on both machines is within 5 minutes. Apparently there is a "maxClockSkew" setting whose default is 5 minutes.

Where's the C# code??      ibhosed ... BobGibsonmrp   |   Edit   |   Show History

In the first few steps, both VB and C# code were shown, but noticed in either step 3 or 4 that someone showed the finished C# code in the comments after the step because of no C# examples, and it has continued thru the last step, no C#. It is selected in my language filter.

I am having problems getting the Client object created because CalculatorClient doesn't exist, so failed build. The error is complicated because of the lack of code for me to compare. I'm a newbie, and this really helps.

Tags What's this?: Add a tag
Flag as ContentBug
C# code      BobGibsonmrp   |   Edit   |   Show History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace Client
{
class Program
{
static void Main(string[] args)
{
//Step 1: Create an endpoint address and an instance of the WCF Client
EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService");
CalculatorClient Client = new CalculatorClient(new WSHttpBinding(), epAddress);
//Step 2: Call the service operations.
//Call the Add service operation.
Double value1 = 100D;
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 = 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.
Client.Close();
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();



}
}
}
Tags What's this?: Add a tag
Flag as ContentBug
Worked perfectly      LHayden ... Thomas Lee   |   Edit   |   Show History
Worked perfectly for me the first time - VS 2008, Windows Server 2003. Nice sample.
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker