Address, Binding and Contract in WCF

Windows communication foundation:

WCF is a programming Platform and run time system for building, running and deploying the network distributed services. It is the latest Service oriented technology. Interoperability is the fundamental characteristics of WCF. It has web service, .Net Remoting, MSMQ and COM+ features. It combines all these things into a single umbrella. Means it provides a common platform for all .Net communication. 

What is ABC?

Without ABC there is no WCF. ABC means Address, Binding and Contract.

Address:

 In WCF, every service is associated with a unique address. The address provides two important elements: the location of the service and the transport protocol or transport schema used to communicate with the service. The location portion of the address indicates the name of the target machine, site, or network; a communication port, pipe, or queue; and an optional specific path or URI. A URI is a Universal Resource Identifier, and can be any unique string, such as the service name or a GUID.

WCF supports following transport schemas

1)      HTTP

2)      TCP

3)      Peer Network

4)      IPC(Inter Process communication)

5)      MSMQ

Addresses have the following format:

[base address]/ [optional URI]
Sample Addresses are:
 
http://localhost:8001
http://localhost:8001/firstservice
net.tcp://localhost:8002/firstservice
net.pipe://localhost/piped
net.msmq://localhost/firstservice
TCP Addresses:
TCP addresses use net.tcp for the transport, and typically include a port number such as: 
net.tcp://localhost:8002/firstservice
When a port number is not specified, the TCP address defaults to port 808.
 
HTTP Addresses

HTTP addresses use http for transport, and can also use https for secure transport. You typically use HTTP addresses with outward-facing Internet-based services, and can specify a port such as:

         http://localhost:8001

     http://localhost:8001/firstservice

When the port number is unspecified, it defaults to 80. Similar to TCP addresses, two HTTP addresses from the same host can share a port, even on the same machine.

IPC Addresses

IPC addresses use net.pipe for transport, to indicate the use of the Windows named pipe mechanism. In WCF, services that use named pipes can only accept calls from the same machine. Consequently, you must specify either the explicit local machine name or localhost for the machine name, followed by a unique string for the pipe name:

 

net.pipe://localhost/piped

You can only open a named pipe once per machine, and therefore it is not possible for two named pipe addresses to share a pipe name on the same machine.

MSMQ Addresses

MSMQ addresses use net.msmq for transport, to indicate the use of the Microsoft Message Queue (MSMQ). You must specify the queue name. When you're dealing with private queues, you must specify the queue type, but that can be omitted for public queues:

net.msmq://localhost/private/firstservice
net.msmq://localhost/firstservice
 
Peer Network Address

Peer network addresses use net.p2p for transport, to indicate the use of the Windows peer network transport. You must specify the peer network name as well as a unique path and port.

----------------------------------------------------------------------------------------------------

Bindings:

Specifies how a service is accessible. In other words: how the two parties will communicate in terms of transport (HTTP, TCP, NamedPipe, Peer2Peer and MSMQ), encoding (text, binary etc.) and protocols (like transactional support or reliable messaging).

Basic binding

Offered by the BasicHttpBinding class, this is designed to expose a WCF service as a legacy ASMX web service, so that old clients can work with new services. When used by the client, this binding enables new WCF clients to work with old ASMX services.

 

TCP binding

Offered by the NetTcpBinding class, this uses TCP for cross-machine communication on the intranet. It supports a variety of features, including reliability, transactions, and security, and is optimized for WCF-to-WCF communication. As a result, it requires both the client and the service to use WCF.

 

Peer network binding

Offered by the NetPeerTcpBinding class, this uses peer networking as a transport. The peer network-enabled client and services all subscribe to the same grid and broadcast messages to it. Peer networking is beyond the scope of this book since it requires an understanding of grid topology and mesh computing strategies.

 

IPC binding

Offered by the NetNamedPipeBinding class, this uses named pipes as a transport for same-machine communication. It is the most secure binding since it cannot accept calls from outside the machine and it supports a variety of features similar to the TCP binding.

 

Web Service (WS) binding

Offered by the WSHttpBinding class, this uses HTTP or HTTPS for transport, and is designed to offer a variety of features such as reliability, transactions, and security over the Internet.

 

Federated WS binding

Offered by the WSFederationHttpBinding class, this is a specialization of the WS binding, offering support for federated security.

 

Duplex WS binding

Offered by the WSDualHttpBinding class, this is similar to the WS binding except it also supports bidirectional communication from the service to the client.

 

MSMQ binding

Offered by the NetMsmqBinding class, this uses MSMQ for transport and is designed to offer support for disconnected queued calls.

 

MSMQ integration binding

Offered by the MsmqIntegrationBinding class, this converts WCF messages to and from MSMQ messages, and is designed to interoperate with legacy MSMQ clients.

----------------------------------------------------------------------------------------------------

Contracts:

In WCF, all services expose contracts. The contract is a platform-neutral and standard way of describing what the service does. WCF defines four types of contracts.

Service contracts:

Describe which operations the client can perform on the service.

 

                         [ServiceContract]
                         interface IFirstContract
                         {
                           [OperationContract]
                           string MyAdd( );
                         }

 

Data contracts:

Define which data types are passed to and from the service. WCF defines implicit contracts for built-in types such as int and string, but you can easily define explicit opt-in data contracts for custom types.

 

                    [DataContract]
                    struct Contact
                    {
                       [DataMember]
                       public string FirstName;
 
                       [DataMember]
                       public string LastName;
                    }

 

Fault contracts:

Define which errors are raised by the service, and how the service handles and propagates errors to its clients.

 

                    [ServiceContract]
                    interface IFirstContract
                    {
                      [OperationContract]
                      [FaultContract(typeof(InvalidOperationException))]
                      [FaultContract(typeof(string))]
                       double MyAdd(double number1,double number2);
                    }

 

Message contracts:

Allow the service to interact directly with messages. Message contracts can be typed or untyped, and are useful in interoperability cases and when there is an existing message format you have to comply with. As a WCF developer, you should use message contracts only rarely, so this book makes no use of message contracts.

 

                    [MessageContract]
                    public class BankingTransaction
                    {
                      [MessageHeader] public Operation operation;
                      [MessageHeader] public DateTime transactionDate;
                      [MessageBodyMember] private Account sourceAccount;
                      [MessageBodyMember] private Account targetAccount;
                      [MessageBodyMember] public int amount;
                    }