Defining and Implementing a WCF Contract                                                

Now that you have seen the ABC’s from a high level, take a look a full example of creating a WCF service.
Assume you have created a new .NET class library project named
WcfMathService with Visual Studio. Also
assume you have set references to
System.ServiceModel.dll and imported the System.ServiceModel
namespace.

The first step is to define the WCF contract exposed by your service. The interface itself is adorned with the
[
ServiceContract] attribute. Each method of the interface is adorned with the [OperationContract]
attribute. You will see further details of these attributes later in the class.

// C#
[ServiceContract()]
public interface IBasicMath
{
[OperationContract]
int Add(int x, int y);

[OperationContract]
int Subtract(int x, int y);
}



' VB
<ServiceContract()> _
Public Interface IBasicMath
<OperationContract()> _
Function Add(ByVal x As Integer, ByVal y As Integer) As Integer

<OperationContract()> _
Function Subtract(ByVal x As Integer, ByVal y As Integer) As Integer
End Interface


Once defined, any class type can implement the interface. WCF will not expose contracts implemented on
structures. They must be class types.

// C#
public class SimpleCalc : IBasicMath
{
public int Add(int x, int y)
{ return x + y; }

public int Subtract(int x, int y)
{ return x - y; }
}


' VB
Public Class SimpleCalc
Implements IBasicMath
Public Function Add(ByVal x As Integer, _
 ByVal y As Integer) As Integer Implements IBasicMath.Add
 Return x + y
End Function

Public Function Subtract(ByVal x As Integer, _
 ByVal y As Integer) As Integer Implements IBasicMath.Add
 Return x - y
End Function
End Class


Recall that strictly speaking, .NET interfaces are not required when defining a WCF contract. The
[ServiceContract] type can be applied to public class types, as well. However, interfaces are strongly
recommended when defining contracts. You have no need to do so, but here is a simple example in C#.

// C# (VB code would be similar).
[ServiceContract]
public class ServiceTypeAsContract
{
[OperationContract]
public void SomeMethod() { /* Some interesting code. */ }

[OperationContract]
public void AnotherMethod() { /* Some interesting code. */ }
}


Once this service has been compiled, you will be able to build any sort of host to expose its services using the
specified bindings. At that point, you can generate proxies for external callers.

You will examine building custom hosts and clients in later chapters. For now, you will leverage a WCF
testing tool,
WcfTestClient.exe. This application functions as a makeshift host and client for testing and
debugging purposes. This GUI-based application allows you to invoke members of a WCF service at the time
of construction.
Defining and Implementing a WCF Contract
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services