[DataContract]
public class CompositeType {
//...
}
In Service1.cs
, define the getAge()
function as follows:
public class Service1 : IService1 {
//...
//...
}
Press F5 to test the application again. This time, select the getAge()
method, enter your name and year of birth, and then click Invoke (see Figure 20-14). Observe the result returned by the service.

Figure 20-14
Consuming the WCF Service
The example you just created is a WCF Service Library. The useful aspect of the project is that it includes the WCF Test Client, which enables you to test your WCF easily without needing to build your own client. In this section, you build a Windows application to consume the service.
Add a Windows Forms Application project to the current solution, and name it ConsumeWCFService
.
Add a service reference to the WCF service created in the previous section. Because the WCF Service is in the same solution as the Windows Forms application, you can simply click the Discover button in the Add Service Reference dialog to locate the service (see Figure 20-15).

Figure 20-15
Use the default ServiceReference1
name, and click OK. Visual Studio 2008 automatically adds the two libraries — System.Runtime.Serialization.dll
and System.ServiceModel.dll
— to your project (see Figure 20-16). The proxy class ServiceReference1
is the reference to the WCF service.

Figure 20-16
Double-click on Form1
, and in the Form1_Load
event handler, code the following:
private void Form1_Load(object sender, EventArgs e) {
}
Calling the WCF service is very similar to consuming an ASMX Web Service — create an instance of the proxy class, call the service's operation, pass in the required parameters, and wait for the result from the service.
Set the Windows Forms application as the startup project, and press F5. A message box appears, displaying 18.
Understanding How WCF Works
Now that you have built your first WCF service, let's take a more detailed look at the innards of WCF.
WCF Communication Protocols
As mentioned earlier, WCF can use a wide variety of transport protocols to transport its messages. Here are just some of the common ones that you can use:
□ HTTP — Much like the traditional ASMX Web Services
□ TCP — Much more flexible and efficient than HTTP; more complex to configure (you'll see an example of this later in this chapter)
□ Named Pipe — Used to communicate with WCF services on the same machine but residing in different processes
□ MSMQ — Uses queuing technology; inherently asynchronous
The ABCs of WCF
Figure 20-17 shows the ABCs of WCF — address, binding, and contract.

Figure 20-17
□ Address — The address that the service is listening at. This indicates
□ Binding — The type of binding that you will use to communicate with the