March 5, 2021
Hot Topics:

Distributed Application Communication Using WCF

  • By Matt Goebel
  • Send Email »
  • More Articles »

Building a WCF client

The final major step in this walkthrough is to create a client to consume the Windows Communication Foundation service that has just been created. The walkthrough will create the client using Windows Presentation Foundation (WPF but it is important to note that the client could be an ASP.NET Web Application, Java application or even another WCF service. The type of client is less important due to WCF's service oriented architecture that abstracts away the communication nightmare that once existed when integrating between multiple systems and platforms.

Open another instance of Visual Studio and create a new WPF project named WCFProductClient. If you are unfamiliar with WPF and feel more comfortable with Windows Forms or Console applications, those are both acceptable alternatives. On the main window create a ListBox control to display the product list data and add a button to delete the selected product from the list. Then create the event handlers for the Window loaded event and the button click event.

Now you will need to generate the proxy that will access the WCF service. There are 2 primary ways this can be accomplished and both yield the same results. The first is to use the Add Service Reference wizard within Visual Studio and the second is to use the ServiceModel Metadata Utility Tool (Svcutil.exe) provided by WCF. If you have the luxury of building a .NET based client and the WCF service publishes metadata as shown in the example above then using the Visual Studio wizard is the quickest and easiest method to generate the proxy. Other cases where the WCF service does not publish metadata then the ServiceModel Metadata Utility Tool will be used to generate the proxy.

The following two examples will walkthrough both methods of generating the proxy. Before using either method make sure to have the WCF service running.

Using the Visual Studio wizard, start by right-clicking on the References folder in the Solution Explorer and selecting the Add Service Reference option. Enter the WCF service's base address and click Go. The wizard will discover the service, exposed endpoints and the operations that those endpoints allow.

Visual Studio Add Service Reference Wizard
Click here for larger image

Figure 1.0 - Visual Studio Add Service Reference Wizard

Enter the Namespace you want to use for the proxy and select OK. Visual Studio will then generate the proxy and add all necessary references in the client project. When finished the client project will be setup to start using the WCF service without further configuration.

Using the ServiceModel Metadata Utility Tool, start by opening a Windows SDK console and navigate to the directory where you want to place the generated proxy code. Using the appropriate switches create the proxy with the following command. Once generated use Visual Studio to add the files to the client project by right-clicking on the solution and selecting Add, Existing Item.

  Svcutil.exe /language:cs /out:ProductManagerService.cs /config:app.config http://localhost:8000/WCFProductService

ServiceModel Metadata Utility Tool command

Regardless of the method used to generate the proxy, once generated it is very easy to configure and consume the WCF service. Using the two events, Windows Loaded and the Button Click, add the following WCF client code.

  private void Window_Loaded(object sender, RoutedEventArgs e)
         ServiceReferenceProductManager.ProductManagerClient pmc = new WCFProductClient.ServiceReferenceProductManager.ProductManagerClient();
         lstProducts.ItemsSource = pmc.GetAllProducts();
  private void btnDeleteProduct_Click(object sender, RoutedEventArgs e)
         ServiceReferenceProductManager.Product selectedProduct                = (ServiceReferenceProductManager.Product)lstProducts.SelectedItem;
         ServiceReferenceProductManager.ProductManagerClient pmc = new WCFProductClient.ServiceReferenceProductManager.ProductManagerClient();
         lstProducts.ItemsSource = pmc.GetAllProducts();


This code populates the ListBox with the product list pulled from the WCF service and allows the user to delete the selected product.


You have just walked through creating a Windows Communication Foundation service by defining a service contract, implementing the contract, configuring and hosting the service then finally consuming the service via a WCF client.

About the Author

Matt Goebel is the Founder and President of AP Innovation, Inc. in Indianapolis, Indiana. He can be reached at 859-802-7591 or matt .goebel@apinnovation.com.

Page 3 of 3

This article was originally published on December 30, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date