Why WCF is Becoming More Popular

Windows Communication Foundation (WCF) is a distributed communication technology first released as a part of the Microsoft .NET Framework 3.0, then enhanced in Framework 3.5.

Prove you're a Dev Guru! Take the test now!

WCF lets you transfer data from one service endpoint to another endpoint. Those service endpoints can be a service hosted in an application or hosted by an IIS (Internet Information Server). An endpoint can be a client of the service, which requests the transfer of data from a service endpoint. WCF integrates features of .NET Remoting, Web Services, MSMQ (Microsoft Message Queuing) and COM+.

WCF supports service-oriented architecture (SOA), which is an underlying structure that has a flexible set of design principles used to support communication between different services. The following positive traits of WCF services make it a popular choice for developers.

Easy Data Representation

We can use WCF DataContractAttribute and DataMemberAttribute for serializing a .NET Framework type’s instances and its fields or properties into XML. The DataContractAttribute can be applied to a class or structure. The DataMemberAttribute can be applied to a field or a property.

For example:

//DataContractAttribute is used for Class & Structure

[DataContract]

public class WCFData

{

//DataMemeberAttribute is used to serialize data1 field

[DataMember]

public string data1;

private string data2;

//DataMemberAttribute is used to serialize Data2 property

[DataMember]

public string Data2

{

get

{

return data2;

}

set

{

data2 = value;

}

}

}

DataContractSerializer is the serializer that WCF uses to serialize .NET types into XML. DataContractSerializer uses the Attributes mentioned above to serialize the instances of .NET types into XML.

Rapid service development

WCF services can be developed very rapidly. WCF services use one or more endpoints. An endpoint can be an address, a binding and a service contract. The address specifies the location of the service. The binding defines how to communicate with the service. The service contract contains the operations that the service can perform.

For example:

[ServiceContract]

public interface IService

{

[OperationContract]

string operation(string input);

}

ServiceContractAttribute specifies that the interface is a WCF Service Contract, whereas OperationContractAttribute specifies which methods of the interface define the operations of the service contract.

Now we need to associate an address and a binding with this service contract. This is generally done in a configuration file. Below is an example of a configuration file.

address="Service"

binding="WSHttpBinding"

contract="IService "/>

Multiple hosting environments

Unlike Web Services that can be only hosted on Internet Information Services (IIS), WCF supports multiple hosting environments. WCF services can be hosted in following ways:

1. Hosting in an application by using ServiceHost class.

2. Hosting in IIS.

3. Hosting in Windows Activation Service (WAS) in Windows Vista.

Descriptive message representation

WCF provides MessageContractAttribute, MessageHeaderAttribute and MessageBodyMemberAttribute to describe the structure of SOAP messages sent and received by the service. To customize the messages exchanged by the services, we use these attributes.

Below is an example to explain how these attributes are applied to yield customized structure of the messages.

.

[DataContract]

public class Header

{

[DataMember]

public int value;

[DataMember]

public string name;

}

[DataContract]

public class Body

{

[DataMember]

public int serialNumber;

[DataMember]

public string ContentName;

[DataMember]

public int cost;

}

[MessageContract]

public class ServiceMessage

{

[MessageHeader]

public Header SOAPHeader;

[MessageBody]

public Body SOAPBody;

}

[ServiceContract]

public interface IService

{

[OperationContract]

public void SendMessage(ServiceMessage serviceMessage);

}