This
article offers a brief explanation on the basic concepts of the
Communication part in the Windows Communication Foundation - WCF. To
further illustrate the concepts, the article provides examples of
configuration settings in the service's web.config file and the client
code.
In order to communicate with a WCF service, the client
needs to know simple details like ABC of the service.
The ABC
of Windows Communication Foundation!!!
Before we learn the
ABC of WCF, we need to know how a WCF service is made accessible to the
clients.
A WCF service allows communication through an Endpoint.
And the endpoint is the only point of communication of the WCF service
that enables message exchange with the client as shown in Figure 1.
Figure 1
And this
Endpoint needs to set the ABC attributes of the WCF service as shown in
Figure 2.
Figure 2
Thus in the WCF
world, ABC is an abbreviation of Address, Binding and Contract
attributes of an Endpoint.
An example of the Endpoint
Let
us quickly run through an example of an endpoint setting in the
web.config file at the service side.
<!--
Endpoint settings in WCF service
-->
<
endpoint
address
=
"
http://localhost:8731/EmployeeWCFService.ServiceImplementation.Manager/
"
binding
=
"basicHttpBinding
"
contract
=
"
EmployeeWCFService.ServiceContract.IEmployee
" />
Where, address is the network address of the service,
binding specifies transport protocol (HTTP, TCP, etc.) selected for the
service and contract is the interface the service implements.
For
the purpose of this article, we are going to emphasize on the Binding
part of the WCF communication mechanism.
So, what is the
Binding?
The Binding is an attribute of an endpoint and it
lets you configure transport protocol, encoding and security
requirements as shown in Figure 3
Figure 3
Types of
Binding
One of the design goals of WCF is to unify the way
distributed systems are developed prior to release of .Net Framework
3.0. WCF offers a single development framework for all scenarios where
distributed solutions were implemented using different technologies such
as ASMX web services, .Net Remoting, COM+, etc. WCF achieves this by
configuring binding attributes of an endpoint. WCF lets you choose HTTP
or TCP transport protocol, encoding, etc. just by tweaking the value of
binding attribute for an endpoint.
To cater to different
transport protocols, WCF lets you select HTTP, TCP and MSMQ binding
types. For the purpose of this article, we are going to concentrate on
HTTP and TCP binding with a concise explanation and simple example
settings.
basicHttpBinding
This type of binding
exists in new .Net world only to support backward compatibility with
ASMX based clients (WS-Basic Profile 1.1). Basic http binding sends SOAP
1.1 messages and is used when there is a requirement for the WCF
services to communicate with non WCF based systems. Thus, providing an
endpoint with basicHttpBinding makes interoperating with other basic
implementations of web services a great choice.
Note: All other
bindings except basicHttpBinding support WS* specifications including
security, reliable messaging and transaction support, where appropriate.
How
to setup the basicHttpBinding?
Let us examine how to setup
binding for an endpoint in the web.config file.
Step 1:
Choose basicHttpBinding as a value in the binding attribute of an
endpoint.
<
endpoint
address
=
"
http://localhost:8731/EmployeeWCFService.ServiceImplementation.Manager/
"
binding
=
"basicHttpBinding
"
bindingConfiguration
=
"basicBinding
"
contract
=
"
EmployeeWCFService.ServiceContract.IEmployee
">
Step
2:
This step is optional and is only required if the binding's
default properties need to be modified, as shown in the example below.
In this case, name the binding same as bindingConfiguration attribute in
the endpoint section.
<
bindings
>|
<
basicHttpBinding
>
<
binding
name
=
"basicBinding
"
textEncoding
=
"utf-8
"
openTimeout
=
"00:03:00
"
closeTimeout
=
"00:03:00
"
/>
</
basicHttpBinding
>
</
bindings
>
Note:
All other types of binding are setup in the same way.
wsHttpBinding
This
binding sends SOAP 1.2 messages and implements WS* specifications to
support enterprise requirements of security, reliability, ordered
delivery and transaction management.
netTcpBinding
This
binding sends SOAP 1.2 messages, provides binary encoding and optimized
communication between WCF services and WCF clients on Windows network.
This binding is the fastest binding amongst all WCF binding options
between different nodes in the TCP network. Unlike http bindings, the
TCP binding does not offer interoperability but is highly optimized for
.Net 3.0 and above clients. Thus, in .Net version 3.0 and above,
providing an endpoint with netTcpBinding is an easy option to
development of distributed systems and can replace COM+ and .Net
Remoting model.
netNamedPipeBinding
This binding is
used to provide secure and reliable Named Pipe based communication
between WCF services and WCF client on the same machine. It is the ideal
choice for communication between processes on the same machine.
netPeerTcpBinding
This
type of binding exists to cater peer-to-peer computing using WCF
services.
For more information on PNRP visit Microsoft page Peer
Name Resolution Protocol
How to setup the
netPeerTcpBinding?
Let us examine how to setup
netPeerTcpBinding for an endpoint in the web.config file.
Step
1:
Before peer-to-peer binding can be used, make sure you have Peer Name Resolution
Protocol
installed on your machine. To enable PNRP on your Windows
XP SP2 and above, take following steps:
-
Go to Add or Remove Programs in the Control Panel
-
Select Add/Remove Windows Components option
-
Select Networking Services from the list of Components and click Details button
-
Select Peer-to-Peer option from the list, as shown in the screenshot, and click the OK button
Additionally, you can also make sure that the PNRP and its dependent services are started as follows:
Step 2:
Choose netPeerTcpBinding as a value in the binding attribute of an
endpoint.
<
endpoint
address
=
"
net.p2p://localhost/MemberWCFService.ServiceImplementation.Member/
"
binding
=
"netPeerTcpBinding
"
bindingConfiguration
=
"netp2pBinding
"
contract
=
"
MemberWCFService.ServiceContract.IMember
">
Step
3:
To configure the binding as shown in the example below.
<
bindings
>
<
netPeerTcpBinding
>
<
binding
name
=
"netP2P
" >
<
resolver
mode
=
"Pnrp
" />
<
security
mode
=
"None
" />
</
binding
>
</
netPeerTcpBinding
>
</
bindings
>
Thumb rules
in choosing endpoint' binding
-
If you require your service to be consumed by clients compatible with SOAP 1.1, use basicHttpBinding for interoperability
-
If you require your service to be consumed within the corporate network, use netTCPBinding for performance
-
If you require your service to be consumed over the internet and the client is a WCF compatible, use wsHttpBinding to reap full benefits of WS* specifications
-
If you require your service to be accessible only in the same machine, use netNamedPipeBinding
-
If you require your service to be queue messages, use netMsmqBinding
-
If you require your service to act as server as well as client in a peer to peer environment, utilise netPeerTcpBinding setting
Binding
configuration summary
Following table shows parameters such
as security, transport protocol, encoding and hosting environment
choices available with different binding for a WCF service. This summary
should help choose an appropriate binding for your project environment.
Binding |
Security
|
Transport Protocol | Encoding
Default Other |
Host |
basicHttpBinding |
None,
|
HTTP | Text/XML, MTOM | IIS, WAS |
wsHttpBinding | Message, Transport, Mixed | HTTP | Text/XML, MTOM | IIS, WAS |
netTcpBinding | Transport, Message, Mixed | TCP | Binary | WAS |
netNamedPipeBinding | Transport, None | Named Pipe | Binary | WAS |
netMsmqBinding | Message, Transport, None | TCP | Binary | WAS |
netPeerTcpBinding | Transport | P2P | Binary | - |
FAQ
about an Endpoint
Can I have multiple
FAQ
about an Endpoint
Can I have multiple endpoints for a
WCF service?
Answer: Yes
Can I have multiple endpoints of the
same binding type e.g. multiple endpoints of basicHttpBinding?
Answer:
Yes
Can I have multiple endpoints of different binding types to
serve different types of clients e.g. an endpoint with
basicHttpBinding, an endpoint with wsHttpBinding and an endpoint with
netTcpBinging?
Answer: Yes
Summary
As you have
seen, Windows Communication Foundation provides a framework to configure
the transport, security and encoding needs to cater various
communication requirements. The WCF framework keeps the development API
same irrespective of the way it is going to be consumed by the clients.
And, the endpoint is provided to meet the communication requirements,
and you can have one or many of them to cater to different clients and
communication requirements. This eliminates the need to learn and
implement diverse set of communication models to support HTTP or TCP
communication infrastructures.