RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


The Baker's Dozen: A 13-Step Crash Course for Learning Windows Communication Foundation (WCF) : Page 2

WCF is meant to make things easier for developers, but as with any new technology, getting started can be difficult. Follow these thirteen steps to rev up your WCF knowledge quickly.

Step 1: Let's Go Shopping for WCF
To get started, you can find background information at the main Microsoft WCF site. I'll also list some great online references for WCF at the end of this article.

If you used .NET interfaces and/or .NET Generics prior to WCF, you're already off to a good start. If you didn't, now is the time!
You'll need to download all the necessary WCF components prior to using the examples in this article. Because the download page links are so long, I'll include them with the sample code that goes with this article.

  1. The Microsoft .NET Framework version 3.0. This version of the .NET Framework contains all the functionality for WCF, as well as Windows Presentation Foundation (WPF) and Windows Workflow Foundation (WF).
  2. The Microsoft Windows SDK for Windows Vista and .NET Framework 3.0 Runtime Components. (These may take a long time to download and install.)
  3. The Visual Studio 2005 Extensions for .NET 3.0 Framework. This allows you to build WCF applications with Visual Studio 2005.
Author's Note: Although you can use XP Professional, you cannot build WCF applications using Visual Studio 2003.

Step 2: Defining the Requirements for the Demo Application
The demo contains two simple classes: a customer class and a customer order status. The customer class returns the information for a specific customer, and the customer order status class returns basic order information for a specific customer.

You'll want to access these two classes using each of the following protocols:

  • TCP from a .NET Windows Forms application (similar to .NET remoting).
  • HTTP from both a .NET Windows Forms and a Web Forms application (and be able to switch back and forth using configuration files).
  • HTTP and SOAP from a non-.NET application.
Step 3: An Overview of WCF Architecture
As much as I'd love to jump in with some code right away, you need to understand some concepts first. At the highest level, you can use WCF for the following:

  • Web services
  • .NET to .NET communication (via HTTP, TCP)
  • Distributed transactions
  • WS-* specifications
  • Queued messaging
Essentially, this means you can use the WCF model for a variety of different areas that previously required different code bases.

WCF contains four key components that I'll define in the next few paragraphs:

  • Contracts
  • Bindings
  • Endpoint definitions
  • Hosting environment
You are probably thinking, "Oh great, contracts…another new term to use!" Well, not exactly. As a teacher of mine used to say (corny joke alert), "It's one of our old friends, dressed in a Halloween costume." In this case, the old friend is the .NET interface, which by definition defines a contract that a class implementing the interface must adhere to. Therefore, a WCF contract defines the operations that a service can perform. Step 4 will cover the specifics of building an interface and establishing WCF contracts.

So if you used .NET interfaces and/or .NET Generics prior to WCF, you're already off to a good start. If you didn't, now is the time!

Bindings are the communication channels between a WCF service and a client (such as TCP, HTTP, etc.). Table 1 lists the WCF bindings, which I'll use throughout the code samples in this article.

Table 1: WCF Bindings











Endpoint definitions refer to the URI (Uniform Resource Identifier) addresses for connecting to a WCF service. An endpoint definition consists of the base address, along with the binding and contract information. The configuration files in Step 5 and Step 6 will contain examples of endpoint definitions. WCF allows you to define as many endpoints as necessary—such as a specific endpoint for a .NET client, and another endpoint for a non-.NET client.

Hosting environments refers to the architecture for hosting WCF services. You can host a WCF service in a .NET application or a Windows service. Additionally, you can host your WCF services using IIS, which I'll cover in Step 9 through Step 11. (Note: At least one of the endpoint definitions must be HTTP.) Finally, you can host a WCF service using the new Windows Activation Service in IIS 7.0.

Here's a summary of what you'll see in the next few steps:

  • In Step 4 through Step 6 you'll learn to build a self-hosted and simple WCF application using configuration files.
  • Step 7 demonstrates how to build the same application without using configuration files. The step will present a little more code to show the WCF object model.
  • In Step 8 you'll create an IIS-hosted WCF service, and then I'll show you how to add a WCF service reference to a .NET client that consumes the service (in the same way that you would have added a Web service reference prior to WCF).
  • Steps 9 through 11 describe another IIS-hosted WCF service, this time for consumption by a non-.NET client.
Step 4: Your First Demo, Building the Interface (Part 1 of 3)
Code time! Your first step is to build the interface for the two methods I described back in Step 2 (GetCustomer and GetOrderHistory). Both will receive a customerID integer as a parameter, and both will return an XML string for the results. You'll construct an interface that establishes the WCF contracts for any clients that consume the service.

TCP remoting in WCF means three words: No more MarshalByRefObject!!!
So do the following:

  1. Fire up Visual Studio 2005 and create a new class library project called DemoInterfaces.
  2. In Solution Explorer, right click on references and add a .NET reference to System.ServiceModel. This is the primary DLL in the .NET 3.0 Framework for WCF. Make sure to add a .NET reference to System.ServiceModel any time you write code using the WCF object model.
  3. Add the code in Listing 1 or Listing 2 (ICustomer.cs or ICustomer.vb).
  4. Build the project, which will create DemoInterfaces.DLL.
The public interface code in Listing 1 and Listing 2 contains two new keyword attributes: ServiceContract and OperationContract.

   public interface ICustomer
      string GetCustomer(int CustomerID);
      string GetOrderHistory(int CustomerID);
The ServiceContact attribute defines the operations that a service can perform, and the OperationContract attribute identifies the exact methods. The .NET CLR translates these interfaces to SOAP types.

You'll use this interface on both the server that hosts the WCF service and the client that uses it. The interface is your contract (pun intended) between the client and the server.

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