Programming with WCF

. Sunday, January 8, 2012
  • Agregar a Technorati
  • Agregar a Del.icio.us
  • Agregar a DiggIt!
  • Agregar a Yahoo!
  • Agregar a Google
  • Agregar a Meneame
  • Agregar a Furl
  • Agregar a Reddit
  • Agregar a Magnolia
  • Agregar a Blinklist
  • Agregar a Blogmarks

The ABCs of WCF: Address, Binding, and Contract
Concisely stated, a WCF service is a collection of endpoints where each endpoint implements
a service contract and contains a binding and an address. Crystal clear, right? Probably not, so
in this section, we elaborate on this definition by illustrating what is meant by contracts, bind-
ings, and addresses.
Defining Service Contracts
Defying convention, a good place to start is with the C, which actually represents service con-
tract. A service contract defines the service operations and the input and output parameters
of each operation. In fact, this notion is extremely similar to the section in WSDL
today. A service contract is typically defined by applying attributes to a class or interface:

using System.ServiceModel
[ServiceContract]
class MathService
{
[OperationContract]
public int Add(int n1, int n2)
{
return n1 + n2;
}
[OperationContract]
private int Subtract(int n1, int n2)
{
return n1 - n2;
}
}
In this example, note the ServiceContract and OperationContract attributes. These attrib-
utes combine to create the service contract. The OperationContract attributes are analogous
to the familiar WebMethod attribute in that each attribute defines the attached method as an
exposed operation. However, notice that the Subtractmethod is private. In WCF, the access
modifiers are orthogonal to the service contract and, therefore, have no bearing on what can
or cannot be exposed outside the service.
These attributes can also be applied to an interface to create the service contract. For
example:
using System.ServiceModel
[ServiceContract]
interface IMathService
{
[OperationContract]
int Add(int n1, int n2);
[OperationContract]
int Subtract(int n1, int n2);
}
class MathService : IMathService
{
public int Add(int n1, int n2)
{
return n1 + n2;
}
public int Subtract(int n1, int n2)
{
return n1 - n2;
}
}
In this example, notice how the implementing class needs no attributes; its only concern is
to implement the IMathService interface. Since it’s desirable to keep the contract separate from
the implementation, using an interface to define the service contract is the preferred approach.

0 comments: