Channels ▼
RSS

.NET

Working with Bindings in WCF 4.5


Windows Communication Foundation (WCF) is Microsoft's unified programming model for building service-oriented applications and the latest release, WCF 4.5, comes with several important features. Here, I discuss some of them, with a particular focus on service contracts, showing how to create a service contract base class in C# 5.0. I also review the supported bindings in WCF, and show how you can design and implement your own custom binding class with additional protocols, security, and so on for use in WCF 4.5. (To work with the code examples discussed in this article, you should use Visual Studio 2012 or later.)

Service Contracts in WCF

Service contracts in WCF are used to expose one or more operations. You can define the operations by creating a method and then using the OperationContractAttribute attribute to expose it. Note that methods that are not decorated with this attribute are not service operations (meaning they are not exposed by your WCF service).

While you can use either an interface or a class to design your service contract, using an interface has several advantages. Interfaces are contracts, and a class implements one or more interfaces. Interfaces are purely abstract classes. The difference between an abstract class and an interface is that an interface can contain only method signatures and no implementation. An abstract class, on the other hand, can contain member objects and also have method implementations, though none of them (neither interface nor abstract class) can be instantiated.

If you use an interface as a service contract, you can extend any number of interfaces. You can then have the service contract implementation in a class that extends this interface. You can change the service contract implementation whenever you need to while keeping the actual service contract the same. You can also version your service by implementing a different interface.

The following code illustrates an interface-based service contract:

[ServiceContract]
public interface ITestService
{
  [OperationContractAttribute]
  public string GetSomeData();
  [OperationContractAttribute]
  public void StoreSomeData();  
}

Bindings in WCF 4.5

Address, Binding, and Contract are commonly referred to as the "ABCs" of WCF. Address is specifies the absolute or relative path or the URI (Uniform Resource Indicator) of the service. In WCF, Binding is used to specify the transport channel that your service uses. Contract specifies the contract that the service endpoint exposes to the outside world. In this section, I focus on the the supported types of Binding in WCF 4.5.

The following are the types of transport channels supported:

  • HTTP
  • TCP
  • Message Queuing
  • Pipes

Table 1 lists the various binding classes in WCF, the transport and message modes, message encoding types, security modes, and transaction support.

Binding Class

Transport

Transport Mode

Message Mode

Message Encoding

Security Mode

Reliable Messaging

Transaction Flow

BasicHttpBinding

HTTP

Yes

Yes

Text

None

Not Supported

Not Supported

WSHttpBinding

HTTP

Yes

Yes

Text

Message

Disabled

WS-AtomicTransactions

WSDualHttpBinding

HTTP

No

Yes

Text

Message

Enabled

WS-AtomicTransactions

WSFederationHttpBinding

HTTP

No

Yes

Text

Message

Disabled

WS-AtomicTransactions

NetTcpBinding

TCP

Yes

Yes

Binary

Transport

Disabled

OleTransactions

NetPeerTcpBinding

P2P

 

 

Binary

Transport

Not Supported

Not Supported

NetNamedPipesBinding

Named Pipes

Yes

No

Binary

Transport

Not Supported

OleTransactions

NetMsmqBinding

MSMQ

Yes

Yes

Binary

Message

Not Supported

Not Supported

MsmqIntegrationBinding

MSMQ

Yes

No

Not Supported

Transport

Not Supported

Not Supported

Table 1: Binding classes in WCF.

Let's now have a quick look at all the supported binding types in WCF.

BasicHttpBinding

BasicHttpBinding is the simplest type of binding used. It provides support for both HTTP and HTTPS protocol. The following code snippet illustrates how this type of binding can be specified.

<bindings>
  <basicHttpBinding>
    <binding name="DDJServiceBinding">
      <security mode="Transport">
        <transport clientCredentialType="None"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

WsHttpBinding

WsHttpBinding is an enhanced form of binding that encrypts the SOAP message. It provides support for both HTTP and HTTPS protocols, and both Text and MTOM encoding methods.

  <binding name=" DDJServiceBinding ">
    <security mode="TransportWithMessageCredential">
      <transport clientCredentialType="None"/>
      <message clientCredentialType="IssuedToken"/>
    </security>
  </binding>

NetTcpBinding

NetTcpBinding is based on the TCP protocol and is a type of binding that provides support for both transactions and security. It uses transport security by default.

  <client>
    <endpoint name="Demo" address="net.tcp://localhost:1234/DDJService" 
      binding="netTcpBinding" contract="IDDJService" >
      <identity>
        <servicePrincipalName value=”DDJService/Joydip-PC” />
      </identity>
    </endpoint>
</client>

NetNamedPipeBinding

In WCF, NetNamedPipeBinding is used for cross-process communication. This type of binding uses transport security and supports message encryption and signing. Here's an example that shows how you can specify this type of binding for your service:

  <services>
  <service name="DDJService" behaviorConfiguration="DDJService Behavior">
          <host>
            <baseAddresses>
              <add baseAddress="net.pipe://localhost:1234/DDJService "/>
            </baseAddresses>
          </host>
  <endpoint address="" binding="netNamedPipeBinding" contract="IDDJService "></endpoint>
  </service>
  </services>

And here is how the netNamedPipeBinding client configuration would look:

<client>
  <endpoint
  address="net.pipe://localhost:1234/DemoService.svc"
  binding="netNamedPipeBinding" contract="IDDJService "></endpoint>
</client>

MsmqIntegrationBinding

MsmqIntegrationBinding is used in situations where you would like to have your service and client interoperate with non-WCF MSMQ clients.

NetMsmqBinding

The NetMsmqBinding type of binding is used in a cross-machine environment. It employs MSMQ as the transport channel.

NetPeerTcpBinding

NetPeerTcpBinding is used for peer-to-peer network applications where services and service clients can intercommunicate. The following code snippet illustrates how you can configure this type of binding:

<endpoint 
  address="net.p2p://localhost:1234/DDJService/"
  binding="netPeerTcpBinding"  
  bindingConfiguration="netp2pBinding"
  contract="IDDJService">
  <bindings>
        <netPeerTcpBinding>
          <binding name="netP2P" >
            <resolver mode="Pnrp" />
            <security mode="None" />
          </binding>
  </netPeerTcpBinding>
</bindings>


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video