This post is part of a series of WCF posts. The following list will updated and published with each post:
- In Depth Windows Communication Foundation (Part 1) - Introduction & WCF Architecture
- In Depth Windows Communication Foundation (Part 2) - Address, Binding & Contract
- In Depth Windows Communication Foundation (Part 3) - Our first Service and Client
- ...
In my previous WCF post, I gave you an introduction and a architectural overview of WCF. It already contained lots of concepts which on first hand might seem complex and unknown. In this post I'd like to explain the WCF mantra a bit more.
The following picture gives a basic understanding of client - service messaging:
You can find this ABC everywhere on the Internet, let me just summarize it for you:
"A" stands for Address: Where is the service?
"B" stands for Binding: How do I talk to the service?
"C" stands for Contract: What can the service do for me?
And so an endpoints is in fact the combination of an Address, a Binding and a Contract.
Remember this:
- The Service can expose itself on multiple endpoints (on different addresses using different bindings). What's a binding? Read on...
- The Client can only reference a service contract on one endpoint conforming the binding used on that endpoint. (of course it can reference other services on other endpoints)
An example of an ABC configuration :
You can see the Address, the Binding, and the Contract described in the endpoint element.
Here is a graphical representation of a service endpoint:
A ServiceEndpoint contains an EndpointAddress (where?), a Binding(how?) and a ContractDescription(what?).
Address
"Provides a unique network address that a client uses to communicate with a service endpoint." (http://msdn2.microsoft.com/en-us/library/system.servicemodel.endpointaddress.aspx)
Binding
A binding "represents a collection of binding elements, each of which describes an aspect of how an endpoint communicates with other endpoints and that are built, consistently, into a channel factory on the client and into a channel listener on the service. A binding contains a collection of binding elements that correspond to protocol channels, transport channels, and message encoders. There can be any number of binding elements for protocol channels but one and only one binding element for each the transport and message encoder." (http://msdn2.microsoft.com/en-us/library/system.servicemodel.channels.binding.aspx)
There is a lot to say about bindings. They make all sorts of transport, security and interoperability happen. WCF provides system-defined bindings and its possible to extend WCF with custom bindings.
First lets take a look what a binding looks like:
So a binding as a Name, a Namespace and a set of BindingElements. A BindingElement is an abstraction of a Channel... and what is a channel?
(There is a nice mini book about WCF Channels available at
http://wcf.netfx3.com/files/folders/product_team/entry3550.aspx)
Quotes from "Channel Model Overview":
A Binding... "is a layered communication stack with one or more channels that process messages. At the bottom of the stack is a transport channel that is responsible for adapting the channel stack to the underlying transport (for example, TCP, HTTP, SMTP and other types of transport.). Channels provide a low-level programming model for sending and receiving messages. This programming model relies on several interfaces and other types collectively known as the WCF channel model."
So a channel is a shackle in the Binding communication chain and allowes us to influence to message passing through.. Let's compare it with a TCP stack:
"First, the similarities: In both cases, each layer of the stack provides some abstraction of the world below that layer and exposed that abstraction only to the layer directly above it. Each layer uses the abstraction of only the layer directly below it. Also in both cases, when two stacks communicate, each layer communicates with the corresponding layer in the other stack, for example, the IP layer communicates with the IP layer and the TCP layer with the TCP layer, and so on.
Now, the differences: While the TCP stack was designed to provide an abstraction of the physical network, the channel stack is designed to provide an abstraction of not only how the message is delivered, that is, the transport, but also other features such as what is in the message or what protocol is used for communication, including the transport "
"Messages flow through the communication stack as Message objects. As shown in figure above, the bottom channel is called a transport channel. It is the channel that is responsible for sending and receiving messages to and from other parties. This includes the responsibility of transforming the Message object to and from the format used to communicate with other parties. Above the transport channel there can be any number of protocol channels each responsible for providing a communication function such as reliable delivery guarantees."
I already mentioned it, WCF gives us a set of system-defined Bindings made up by system defined BindingElements. All of these BindingElements can in a way be reused in custom Bindings and of course we can write our own BindingElements/Channels to make up new Bindings!
A complete list of standard WCF bindings (including the new 3.5 ones) is given here.
Wow.. does your developers heart beats twice as fast now? :) This model provides us endless extensiblity options! and believe me, this is just the beginning, because 'extensibility' is WCF's middle name!
All services out there implement some communication pattern... Often they are called Message Exchange Pattern's (MEP's) but within WCF documentation they are often refered to as Shapes.
You propably recognize some of them :
- One-Way - the client is the initiator and messages go from client to service.
- Request-Response - the client is the initiator and sends a message after which a response is sent back
- Duplex - both client and service can be the initiator and messages flow back and forward.
A channel always implements one or more of these Shape interfaces: IInputChannel/IOutputChannel/IDuplexChannel and IReplyChannel/IRequestChannel for request/response pattern.
Contract
"A WCF Contract is a collection of Operations that specifies what the Endpoint communicates to the outside world." (http://msdn2.microsoft.com/en-us/library/aa480210.aspx)
Remember that SOA tenet "Services share schema and contract, not class or type" Don gave us in my previous post? This is the service contract. This way, nice interoperable messaging can be established between all sorts of communication stacks based on different plantform technologies. These stacks of course have to comply to some mutual standards; for example SOAP, WSDl and WS-* standards. I'll write more about those subjects later..
A ServiceContract might look like this:
1 [ServiceContract]
2 public interface IService1
3 {
4 [OperationContract]
5 string GetData(int value);
6
7 [OperationContract]
8 CompositeType GetDataUsingDataContract(CompositeType composite);
9 }
(pictures from http://msdn2.microsoft.com/en-us/library/aa480210.aspx)
Next time, I'll start working with a code example which I'll use throughout all next posts.
More to read in my next post in this series:
In Depth Windows Communication Foundation (Part 3) - Our first Service and Client