In Windows Communication Foundation (WCF), which is a framework for building service-oriented applications, key concepts like Service Contract, Data Contract, XML, and WCF Bindings play a central role in defining how services communicate, exchange data, and how these interactions are structured.
Let's break down these concepts:
A Service Contract in WCF is an interface or a set of operations (methods) that define what the service does. It acts as the contract or agreement between the service provider (the server) and the service consumer (the client). This contract defines the methods that can be called, the input and output parameters, and the exception handling.
[ServiceContract] attribute.[OperationContract] attribute.[ServiceContract]
public interface ICalculatorService
{
[OperationContract]
int Add(int num1, int num2);
[OperationContract]
int Subtract(int num1, int num2);
}
ICalculatorService interface defines a service contract with two operations: Add and Subtract.[ServiceContract] attribute declares that this interface is a service contract.Add and Subtract, is annotated with the [OperationContract] attribute.A Data Contract in WCF is a formal agreement between the service and the client regarding the structure of the data that will be exchanged. It defines the shape of the data that is sent and received by the service. This contract ensures that both the service and client agree on how the data will be serialized and deserialized.
[DataContract] attribute.[DataMember] attribute.[DataContract]
public class Customer
{
[DataMember]
public int CustomerId { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Email { get; set; }
}
Customer class defines a data contract.[DataContract] attribute marks the class as a data contract.[DataMember] attribute is applied to each property that needs to be serialized and sent over the network.In WCF, XML plays an important role because data is typically transmitted in XML format over the wire. Both the service and the client exchange data using XML-encoded messages. WCF uses SOAP (Simple Object Access Protocol) as the default messaging format, which is XML-based.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:web="http://www.example.com/webservice">
<soapenv:Header/>
<soapenv:Body>
<web:Add>
<web:num1>5</web:num1>
<web:num2>3</web:num2>
</web:Add>
</soapenv:Body>
</soapenv:Envelope>
Add) and the parameters (num1 and num2).WCF Bindings define how the service communicates with the client. A binding is a set of protocols and transport mechanisms used for communication between the client and the service. It determines how the service should be accessed, the format of the messages, and the transport protocol (e.g., HTTP, TCP).
WCF provides several built-in bindings that cater to different communication scenarios:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicHttpBindingConfig">
<security mode="None"/>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="CalculatorService">
<endpoint address=""
binding="basicHttpBinding"
bindingConfiguration="basicHttpBindingConfig"
contract="ICalculatorService"/>
</service>
</services>
</system.serviceModel>
basicHttpBinding for communication between the client and the service.<security mode="None"/> indicates no security (but this can be configured for more secure communications).ICalculatorService contract is exposed by the CalculatorService service.[ServiceContract] attribute, and each method is marked with the [OperationContract] attribute.[DataContract] attribute, and individual properties are marked with the [DataMember] attribute.BasicHttpBinding, NetTcpBinding, and WsHttpBinding.By using Service Contracts, Data Contracts, XML messages, and Bindings, WCF provides a robust and flexible platform for building distributed, service-oriented applications that can communicate across platforms.
Open this section to load past papers