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.
Note: |
---|
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
-
Create a new project within the current solution for the client in by doing the following steps:
- 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.
- 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.
- Click OK.
- 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.
-
Add a reference to the System.ServiceModel.dll for the project:
- Right-click the References folder under the Client project in the Solution Explorer and select Add Reference.
- 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.
Note: 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. - Right-click the References folder under the Client project in the Solution Explorer and select Add Reference.
-
Add a using statement (Imports in Visual Basic) for the System.ServiceModel namespace in the generated Program.cs or Program.vb file.
Visual BasicImports System.ServiceModel;
C#using System.ServiceModel;
-
Start the service created in the previous steps. For more information, see How to: Host and Run a Basic Windows Communication Foundation Service.
-
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:
- Start a Windows SDK console session by selecting CMD Shell under the Microsoft Windows SDK entry in the Start menu.
- 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.
- 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
svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/ServiceModelSamples/service
- Start a Windows SDK console session by selecting CMD Shell under the Microsoft Windows SDK entry in the Start menu.
-
Add the generated proxy (generatedProxy.cs )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).
//------------------------------------------------------------------------------ // <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.