How to: Create a Client
How to: Create a Windows Communication Foundation Client

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 the ServiceModel 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.

ms733133.note(en-us,VS.90).gifNote:
You can add a service reference to your client project inside Visual Studio 2008 to create the client proxy instead of using the ServiceModel Metadata Utility Tool (Svcutil.exe).

The client application uses the generated proxy to create an 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 by doing the following steps:

    1. In Solution Explorer (on the upper right) within the same solution that contains the service, right-click the current solution (not the project), and select Add, and then New Project.

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

    3. Click OK.

  2. Add a reference to the System.ServiceModel.dll for the project:

    1. Right-click the References folder under the Client project in the Solution Explorer and select Add Reference.

    2. Select the Recent tab and select System.ServiceModel.dll from the list box and click OK. Because you already added a reference to this assembly in the first step of this tutorial, it is now listed in the Recent tab. If you do not see it in the Recent tab, select the Browse tab and navigate to C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation and select the assembly from there.

    ms733133.note(en-us,VS.90).gifNote:
    When using a command-line compiler (for example, Csc.exe or Vbc.exe), you must also provide the path to the assemblies. By default, on a computer running Windows Vista for example, the path is: Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation.

  3. Add a using statement (Imports in Visual Basic) for the System.ServiceModel namespace in the generated Program.cs or Program.vb file.

    Visual Basic
    Imports System.ServiceModel;
    C#
    using System.ServiceModel;
  4. Start the service created in the previous steps. For more information, see How to: Host and Run a Basic Windows Communication Foundation Service.

  5. Run the Service Model Metadata Utility Tool (SvcUtil.exe) with the appropriate switches to create the client code and a configuration file by doing the following steps:

    1. Start a Windows SDK console session by selecting CMD Shell under the Microsoft Windows SDK entry in the Start menu.

    2. 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>\Documents\Visual Studio 2005\Projects\Service\Client.

    3. Use the command-line tool Service Model 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.

      [Visual Basic]

      svcutil.exe /language:vb /out:generatedProxy.vb /config:app.config http://localhost:8000/ServiceModelSamples/service
      [C#]

      svcutil.exe /language:cs /out:generatedProxy.cs /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>\Documents\Visual Studio 2005\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 Service Model Metadata Utility Tool (Svcutil.exe).

Visual Basic
'------------------------------------------------------------------------------
' <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/ICalculator/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/ICalculator/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/ICalculator/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/ICalculator/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
C#
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------



[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/ICalculator/Add", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")]
    double Add(double n1, double n2);
    
    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Subtract", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")]
    double Subtract(double n1, double n2);
    
    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Multiply", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")]
    double Multiply(double n1, double n2);
    
    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Divide", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")]
    double Divide(double n1, double n2);
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface ICalculatorChannel : ICalculator, System.ServiceModel.IClientChannel
{
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class CalculatorClient : System.ServiceModel.ClientBase<ICalculator>, ICalculator
{
    
    public CalculatorClient()
    {
    }
    
    public CalculatorClient(string endpointConfigurationName) : 
            base(endpointConfigurationName)
    {
    }
    
    public CalculatorClient(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress)
    {
    }
    
    public CalculatorClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress)
    {
    }
    
    public CalculatorClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress)
    {
    }
    
    public double Add(double n1, double n2)
    {
        return base.Channel.Add(n1, n2);
    }
    
    public double Subtract(double n1, double n2)
    {
        return base.Channel.Subtract(n1, n2);
    }
    
    public double Multiply(double n1, double n2)
    {
        return base.Channel.Multiply(n1, n2);
    }
    
    public double Divide(double n1, double n2)
    {
        return base.Channel.Divide(n1, n2);
    }
}

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.

See Also


© 2007 Microsoft Corporation. All rights reserved.
Build Date: 2009-10-13
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Actual output of svcutil.exe doesn't go to the directory specified.      Cathy Brennan - MSFT   |   Edit   |   Show History

When I built the proxy by using svcutil, the output didn't show up where this topic indicated it would. (I'm running on Windows Server 2003.) Instead, it was up a level, in the \Client directory.

"... 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>\Documents\Visual Studio 2005\Projects\Service\Client directory."

Tags What's this?: Add a tag
Flag as ContentBug
Wrong URI for svcutil      Jeff Certain ... Thomas Lee   |   Edit   |   Show History

The URI provided to svcutil is wrong:

svcutil.exe /language:vb /out:generatedProxy.vb /config:app.config http://localhost:8000/ServiceModelSamples/service


should be:

svcutil.exe /language:vb /out:generatedProxy.vb /config:app.config http://localhost:8000/ServiceModelSamples/services
Not all of us use curly braces...      Jeff Certain   |   Edit   |   Show History
Step 6 has an error:

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.cs file generated in the preceding step.

I generated a vb file.
After Step 5 !      AmolWankhede   |   Edit   |   Show History

After step 5, step 6 tells you to add a file in project. However we have the same solution and Service is running. I guess you cant add a file in project/solution that is being debugged. (I assume most of the readers will debug the service that they have created instead of running it from the command prompt as it is not specified so)

Epic Fail      HippyInASuit2 ... Thomas Lee   |   Edit   |   Show History
This is pretty freaking stupid! I have to use a command line utility to generate the equivelent of code that VS 2005 was able to generate for me. Defining these proxy classes with the same names messes everything up unless I want to manually change around my namespaces. This whole process is ungodly stupid!
Flag as ContentBug
Output goes to directory you run svcutil.exe from      MaryQ   |   Edit   |   Show History
There is a reason the instructions in Step 5b say "Navigate to the directory where you want to place the client code." Because that's where it goes! It also states what the default directory is...and then later, in Step 5c, assumes you placed your project in that default directory. The generated files go into the directory you ran svcutil from, whatever that directory is.
Worked perfect      Janus007 ... Thomas Lee   |   Edit   |   Show History
I cannot understand all these complains.

Wrong URI for svcutil -> No it wasn't, just remember to get this one correct: Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");

Actual output of svcutil.exe doesn't go to the directory specified.-> Just navigate to your output directory and everything works fine!

Not all of us use curly braces... -> Don't cry!

After Step 5 !-> w00t ?? This is childish...

Epic Fail-> yeah yeah.. not everything is drag and drop, hopefully MSFT will create more cmd-utils. I'm tired of people that cries when they are forced to use the keyboard to program *LOL*



Flag as ContentBug
It's worked, attention to output files      Jinn CoOl ... Thomas Lee   |   Edit   |   Show History
It works,

In my case, for some reason I have SDK v6.0A and powershell installed. Under start menu I have NO CMD shell, and I have to use window standard cmd.

It took me sometimes to figure out how to generate the proxy file using standard cmd & svcutil.

Inside cmd, I have to navigate to the SDK folder bin by using:
cd C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin

in this directory you can find svcutil.exe, and you can run:
svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/ServiceModelSamples/service

after running this command, the output file is located right in the recent folder.
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin

I have to go there and copy it to my Client project and add to the solution later.

This all sound logical to me, because I haven't seen how this svcutil knows where my client project is located.

Just in case someone need to refer

JINN
Tags What's this?: comment (x) wcf (x) Add a tag
Flag as ContentBug
Alternate CMD Shell      sak24   |   Edit   |   Show History
Instead of using the Windows SDK CMD Shell, you can use the Visual Studio 2008 Command Prompt, which is found in the Start | Programs | Microsoft Visual Studio 2008 | Visual Studio Tools start menu folder.
Tags What's this?: Add a tag
Flag as ContentBug
If svcutil.exe throws exception, this page might help...      WickedSmaht Solutions   |   Edit   |   Show History
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=289442&wa=wsignin1.0

It says svcutil.exe was never signed. So you may want to run 'sn -v "c:\Program Files\Microsoft SDKs\Windows\V6.0A\Bin\svcutil.exe"' first.

It works      ChristianProgrammer2 ... Thomas Lee   |   Edit   |   Show History
It worked. It's just an ugly process, Juval Lowy wouldnt do it like this !!!!! :-)
Flag as ContentBug
Wow...people are dumb      Dave Doubi ... Thomas Lee   |   Edit   |   Show History
Wow...I am glad I am not Microsoft to be dealing with such a mass of newbs.

Such a simple and elegant tutorial, such dumb whiners...So you want me to ask you to not forget to breath as well???

Just read the "Worked Perfect" (the only smart comment in this whole section) there you go. It works as advertised.
Flag as ContentBug
svcutil can be told, where to put the output file,      chrome-cobalt   |   Edit   |   Show History
It's the /directory switch. Look here: http://msdn.microsoft.com/en-us/library/ms734663.aspx
Tags What's this?: Add a tag
Flag as ContentBug
Using Visual Studio 2008, Creating WCF client much easier      James Wjh   |   Edit   |   Show History
In VS2008, Creating WCF client is much much easier, no proxy, no cmd tool, just click "Add reference" and paste your wsdl link :http://localhost:8000/ServiceModelSamples/Service?wsdl, click "Go", and you have a new namespace CalculatorService and new class CalculatorClient, like this: CalculatorService. CalulatorClient cc = new CalculatorService.CalculatorClient(); Console.WriteLine("Add 123 and 245 is {0}", cc.Add(123.0, 245.0); Console.ReadLine();


Tags What's this?: client (x) creating (x) in (x) vs2008 (x) wcf (x) Add a tag
Flag as ContentBug
it worked      lovray   |   Edit   |   Show History
Ya I agree with James Wjh: "Using Visual Studio 2008, Creating WCF client much easier"

add "service" reference:
http://localhost:8000/ServiceModelSamples/Service


Tags What's this?: Add a tag
Flag as ContentBug
Processing
Page view tracker