You are on page 1of 8

http://msdn.microsoft.com/en-us/library/ms733133.

aspx

How to: Create a Windows Communication


Foundation Client
.NET Framework 4
Other Versions

This is the fourth 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.

This topic describes how to retrieve metadata from a WCF service and use it to create a WCF proxy
that can access the service. This task is completed by using theServiceModel Metadata Utility Tool
(Svcutil.exe) provided by WCF. This tool obtains the metadata from the service and generates a
managed source code file for a proxy in the language you have chosen. In addition to creating the
client proxy, the tool also creates the configuration file for the client that enables the client application
to connect to the service at one of its endpoints.

Note:
You can add a service reference to your client project inside Visual Studio 2010 to create the client proxy instead of us
Metadata Utility Tool (Svcutil.exe).

Caution:
When calling a WCF service from a class library project in Visual Studio 2010 you can use the Add Service Reference
generate a proxy and associated configuration file. The configuration file will not be used by the class library project. Y
configuration file to the directory that contains the executable that will call the class library.

The client application uses the generated proxy to create a WCF client object. This procedure is
described in How to: Use a Windows Communication Foundation Client.

The code for the client generated by this task is provided in the example following the procedure.

To create a Windows Communication Foundation


client
1. Create a new project within the current solution for the client in Visual Studio 2010 by doing the
following steps:
a. In Solution Explorer (on the upper right) within the same solution that contains the
service, right-click the current solution (not the project), and selectAdd, and then New
Project.

b. In the Add New Project dialog, select Visual Basic or Visual C#, and choose
the Console Application template, and name it Client. Use the defaultLocation.

c. Click OK.
2. Add a reference to the System.ServiceModel.dll for the project:
a. Right-click the References folder under the Client project in the Solution
Explorer and select Add Reference.

b. Select the .NET tab and select System.ServiceModel.dll (version 4.0.0.0) from the list
box and click OK.

Note:
When using a command-line compiler (for example, Csc.exe or Vbc.exe), you must also provide the path to the
a computer running Windows Vista for example, the path is: Windows\Microsoft.NET\Framework\v4.0.
3. Add a using statement (Imports in Visual Basic) for the System.ServiceModel namespace in
the generated Program.cs or Program.vb file.

VB

C#

C++

F#

JScript

Copy

Imports System.ServiceModel

4. In Visual Studio, press F5 to start the service created in the previous topic. For more
information, see How to: Host and Run a Basic Windows Communication Foundation Service.
5. Run the ServiceModel Metadata Utility Tool (Svcutil.exe) with the appropriate switches to create
the client code and a configuration file by doing the following steps:
a. On the Start menu click All Programs, and then click Visual Studio 2010.
Click Visual Studio Tools and then click Visual Studio 2010 Command Prompt.

b. Navigate to the directory where you want to place the client code. If you created the
client project using the default, the directory is C:\Users\<user name>\My
Documents\Visual Studio 10\Projects\Service\Client.

c. Use the command-line tool ServiceModel Metadata Utility Tool (Svcutil.exe) with the
appropriate switches to create the client code. The following example generates a code
file and a configuration file for the service.
VB

C#

C++

F#

JScript

Copy

svcutil.exe /language:vb /out:generatedProxy.vb /config:app.config


http://localhost:8000/ServiceModelSamples/service

By default, the client proxy code is generated in a file named after the service (in this
case, for example, CalculatorService.cs or CalculatorService.vb where the extension is
appropriate to the programming language: .vb for Visual Basic or .cs for C#).
The /out switch changes the name of the client proxy file to GeneratedProxy.cs.
The /config switch changes the name of the client configuration file from the default
Output.config to App.config. Note that both of these files get generated in the
C:\Users\<user name>\My Documents\Visual Studio 10\Projects\Service\Client directory.

6. Add the generated proxy to the client project in Visual Studio, right-click the client project
in Solution Explorer and select Add and then Existing Item. Select the generatedProxy file
generated in the preceding step.

Example
This example shows the client code generated by the ServiceModel Metadata Utility Tool (Svcutil.exe).

VB
C#
C++
F#
JScript
Copy

'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:2.0.50727.1366
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict Off


Option Explicit On

<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0"), _
System.ServiceModel.ServiceContractAttribute([Namespace]:="http://Microsoft.ServiceModel.Samples
", ConfigurationName:="ICalculator")> _
Public Interface ICalculator

<System.ServiceModel.OperationContractAttribute(Action:="http://Microsoft.ServiceModel.Samples/I
Calculator/Add", ReplyAction:="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")> _
Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double

<System.ServiceModel.OperationContractAttribute(Action:="http://Microsoft.ServiceModel.Samples/I
Calculator/Subtract",
ReplyAction:="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")> _
Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double

<System.ServiceModel.OperationContractAttribute(Action:="http://Microsoft.ServiceModel.Samples/I
Calculator/Multiply",
ReplyAction:="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")> _
Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double

<System.ServiceModel.OperationContractAttribute(Action:="http://Microsoft.ServiceModel.Samples/I
Calculator/Divide",
ReplyAction:="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")> _
Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double
End Interface

<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")> _
Public Interface ICalculatorChannel
Inherits ICalculator, System.ServiceModel.IClientChannel
End Interface

<System.Diagnostics.DebuggerStepThroughAttribute(), _
System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")> _
Partial Public Class CalculatorClient
Inherits System.ServiceModel.ClientBase(Of ICalculator)
Implements ICalculator

Public Sub New()


MyBase.New
End Sub

Public Sub New(ByVal endpointConfigurationName As String)


MyBase.New(endpointConfigurationName)
End Sub

Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As String)


MyBase.New(endpointConfigurationName, remoteAddress)
End Sub

Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As


System.ServiceModel.EndpointAddress)
MyBase.New(endpointConfigurationName, remoteAddress)
End Sub

Public Sub New(ByVal binding As System.ServiceModel.Channels.Binding, ByVal remoteAddress As


System.ServiceModel.EndpointAddress)
MyBase.New(binding, remoteAddress)
End Sub

Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add


Return MyBase.Channel.Add(n1, n2)
End Function

Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double Implements


ICalculator.Subtract
Return MyBase.Channel.Subtract(n1, n2)
End Function

Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double Implements


ICalculator.Multiply
Return MyBase.Channel.Multiply(n1, n2)
End Function

Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double Implements


ICalculator.Divide
Return MyBase.Channel.Divide(n1, n2)
End Function
End Class

Now you have created a Windows Communication Foundation (WCF) client. Proceed to How to:
Configure a Basic Windows Communication Foundation Client to configure the client. For
troubleshooting information, see Troubleshooting the Getting Started Tutorial.

How to: Use a Windows Communication Foundation


Client
.NET Framework 4
Other Versions
This is the last 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:

1. Creates a WCF client.

2. Calls the service operations from the generated proxy.

3. 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.

VB

C#

C++

F#

JScript

Copy

' 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)

2. Call the client operations from within the Client.


VB

C#

C++

F#

JScript

Copy

'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)

3. Call Close on the WCF client and wait until the user presses ENTER to terminate the application.
VB

C#

C++

F#

JScript

Copy

' 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

You might also like