A binding describes how a service wants to communicate. With binding, you can specify the following features.
Transport protocol
Security requirements
Encoding format
Transaction requirements
A binding is composed of multiple binding elements that describe all binding requirements. You can create a custom binding or use one of the predefined bindings that are shown in the following table.
Standard Binding | Description |
---|---|
BasicHttpBinding | BasicHttpBinding is the binding for the broadest interoperability, the first-generation Web services. Transport protocols used are HTTP or HTTPS; security is available only from the protocol. |
WSHttpBinding | WSHttpBinding is the binding for the next-generation Web services, platforms that implement SOAP extensions for security, reliability, and transactions. The transports used are HTTP or HTTPS; for security the WS-Security specification is implemented; transactions are supported, as has been described, with the WS-Coordination; WS-AtomicTransaction and WS-BusinessActivity specifications; reliable messaging is supported with an implementation of WS-ReliableMessaging. WSProfile also supports MTOM encoding for sending attachments. |
WSFederationHttpBinding | WSFederationHttpBinding is a secure and interoperable binding that supports sharing identities across multiple systems for authentication and authorization. |
WSDualHttpBinding | The binding WSDualHttpBinding, in contrast to WSHttpBinding, supports duplex messaging. |
NetTcpBinding | All standard bindings prefixed with the name Net use a binary encoding used for communication between .NET applications. This encoding faster than the text encoding with WSxxx bindings. The binding NetTcpBinding uses the TCP/IP protocol. |
NetPeerTcpBinding | NetPeerTcpBinding provides a binding for peer-to-peer communication. |
NetNamedPipeBinding | NetNamedPipeBinding is optimized for communication between different processes on the same system. |
NetMsmqBinding | The binding NetMsmqBinding brings queued communication to WCF. Here the messages are sent to the message queue. |
MsmqIntegrationBinding | MsmqIntegrationBinding is the binding for existing applications that make use of message queuing. Contrary, the binding NetMsmqBinding requires WCF applications both on the client and server. |
CustomBinding | With a custom binding the transport protocol, security requirements can be completely customized. |
Depending on the binding different features are supported. The bindings starting with WS are platform-independent, supporting Web services specifications. Bindings that start with the name Net use binary formatting for high-performance communication between .NET applications. Other features are support of sessions, reliable sessions, transactions, and duplex communication; the following table lists the bindings supporting these features.
Feature | Binding |
---|---|
Sessions | WSHttpBinding, WSDualHttpBinding, WsFederationHttpBinding, NetTcpBinding, NetNamedPipeBinding |
Reliable Sessions | WSHttpBinding, WSDualHttpBinding, WsFederationHttpBinding, NetTcpBinding |
Transactions | WSHttpBinding, WSDualHttpBinding, WSFederationHttpBinding, NetTcpBinding, NetNamedPipeBinding, NetMsmqBinding, MsmqIntegrationBinding |
Duplex Communication | WsDualHttpBinding, NetTcpBinding, NetNamedPipeBinding, NetPeerTcpBinding |
Along with defining the binding, the service must define an endpoint. The endpoint is dependent on the contract, the address of the service, and the binding. In the following code sample, a ServiceHost object is instantiated, and the address http://localhost:8080/RoomReservation, as well as a WsHttpBinding instance, is added to an endpoint of the service.
static ServiceHost host; static void StartService() { Uri baseAddress = new Uri("http://localhost:8080/RoomReservation"); host = new ServiceHost( typeof(RoomReservationService)); WSHttpBinding binding1 = new WSHttpBinding(); host.AddServiceEndpoint(typeof(IRoomService), binding1, baseAddress); host.Open(); }
Besides defining the binding programmatically, you can also define it with the application configuration file. The configuration for WCF is placed inside the element <system.serviceModel>. The <service> element defines the services offered. Similarly, as you’ve seen in the code, the service needs an endpoint, and the endpoint contains address, binding, and contract information. The default binding configuration of wsHttpBinding is modified with the bindingConfiguration XML attribute that references the binding configuration wsHttpConfig1. This is the binding configuration you can find inside the <bindings> section, which is used to change the wsHttpBinding configuration to enable reliableSession.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="Wrox.ProCSharp.WCF.RoomReservationService"> <endpoint address=" http://localhost:8080/RoomReservation" contract="Wrox.ProCSharp.WCF.IRoomService" binding="wsHttpBinding" bindingConfiguration="wsHttpConfig1" /> </service> </services> <bindings> <wsHttpBinding> <binding name="wsHttpConfig1"> <reliableSession enabled="true" /> </binding> </wsHttpBinding> </bindings> </system.serviceModel> </configuration>