[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite
// types to service operations
[DataContract]
public class CompositeType {
bool boolValue = true;
string stringValue = 'Hello ';
[DataMember]
public bool BoolValue {
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue {
get { return stringValue; }
set { stringValue = value; }
}
}
}
Here, there is an interface (IService1
) and a class (CompositeType
) defined. The IService1
interface is set with the [ServiceContract]
attribute to indicate that this is a service contract and contains signatures of operations exposed by the service. Within this interface are signatures of methods that you will implement in the Service1.cs
file. Each method is set with the [OperationContract]
attribute to indicate that it is an operation. If you have additional operations to add, you can add them here.
The CompositeType
class is prefixed with the [DataContract]
attribute. This class defines the various composite data types required by your service.
The Service1.cs
file contains the implementation for the operations defined in the IService1
interface in the IService1.cs
file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfServiceLibraryTest {
// NOTE: If you change the class name 'Service1' here, you must also update the
// reference to 'Service1' in App.config.
public class Service1 : IService1 {
public string GetData(int value) {
return string.Format('You entered: {0}', value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite) {
if (composite.BoolValue) {
composite.StringValue += 'Suffix';
}
return composite;
}
}
}
For now, use the default implementation provided by Visual Studio 2008 and examine how the service works.
Press F5 to debug the service. A WCF Test Client window will be displayed (see Figure 20-9). This is a test client shipped with Visual Studio 2008 to help you test your WCF service.

Figure 20-9
Expand the IService1
item, and select the GetData()
method. In the right of the window, enter 5 for the value and click the Invoke button (see Figure 20-10).

Figure 20-10
When you see a security warning dialog, click OK. The service returns its result in the Response pane (see Figure 20-11).

Figure 20-11
Also, try the GetDataUsingDataContract()
operation and enter some values as shown in Figure 20-12. Click Invoke, and observe the results returned.

Figure 20-12
You can also see the SOAP messages exchanged between the test client and the service by clicking on the XML tab (see Figure 20-13).

Figure 20-13
Notice that the SOAP messages contain a lot more information than a traditional ASMX Web Service SOAP packet. This is because WCF services, by default, use wsHttpBinding
, which ensures that information exchanged between the client and the service is encrypted automatically.
You'll see more about wsHttpBinding
later in this chapter.
Close the WCF Test Client window. Back in Visual Studio 2008, edit the IService1.cs
file, adding the getAge()
function signature to the IService1
interface:
[ServiceContract]
public interface IService1 {
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
}
By default, the [OperationContract]
attribute specifies a request/response messaging pattern for the operation.
After the class definition for CompositeType
, define the following data contract called Contact
: