WCF RESTful Service      Interview Questions         WCF Bindings             WCF Tracing                              

Monday, July 30, 2012

WCF Services - Choosing the appropriate WCF binding

Windows Communication Foundation is a programming model that enables us to develop and then expose our services in a variety of different ways. It means a single WCF service can be exposed with different wire formats and message protocols. Bindings in WCF actually defines that how to communicate with the service. So, binding specifies that what communication protocol as well as encoding method will be used. Optionally, it can specify other important factors like transactions, reliable sessions and security.


Windows Communication Foundation comes with a number of built-in bindings that we can use to expose our service, but WCF is extensible so we can define our own custom bindings to fulfill specific needs.

In this WCF Tutorial, we will keep our focus on learning about existing bindings available in WCF. But instead of explaining those bindings in general, we will try to understand these bindings with respect to some suitable scenarios in which it can be used. For understanding WCF Binding internals, another WCF Binding Tutorial has these details.

Interoperability Scenario:
If we are going to develop a WCF service that will be consumed by non-WCF client applications, then we can expose our service using BasicHttpBinding or WsHttpBinding. So, how these two bindings differ from each other is explain as:
1)    BasicHttpBinding is designed to replace ASMX Web services. It supports both HTTP and Secure HTTP. As far as encoding is concerned, it provides support for Text as well as MTOM encoding methods. BasicHttpBinding doesn’t support WS-* standards like WS-Addressing, WS-Security and WS-ReliableMessaging.

2)    WsHttpBinding also supports interoperability. With this binding, the SOAP message is, by default, encrypted. It supports HTTP and HTTPS. In terms of encoding, it provides support for Text as well as MTOM encoding methods. It supports WS-* standards like WS-Addressing, WS-Security and WS-ReliableMessaging. By default, reliable sessions are disabled because it can cause a bit of performance overhead.

3)    WsDualHttpBinding has all features of WsHttpBinding with addition that it supports Duplex MEP (Message Exchange Pattern). In this MEP, service can communicate with client via callback. Its basically a two way communication.

Note: For further details on Message Exchange Patterns, you can refer my other article “WCF Service -Top 10 Interview Questions” which explains MEPs in details.

4)    WsFederationHttpBinding is a specialized form of WS Binding that offers support for federated security.

Single Computer Scenario:
If our WCF service resides on a single computer, then netNamedPipeBinding will be the best choice.
5)    NetNamedPipeBinding is secure and reliable binding on a single WCF computer across process communication. It provides support for binary encoding which is the best choice in this scenario and uses named pipes as transport for SOAP messages.

Intranet/Cross Computers .NET Communication Scenario:
If we need to communicate across computers with same .NET technology on intranet, then netTcpBinding or netPeerTcpBinding options are available. It’s basically the replacement or enhancement of earlier .NET Remoting technology.
6)    NetTcpBinding supports reliability, transactions and security. It also supports TCP protocol and binary as encoding method. We can say that it’s the most optimized or fastest binding because both client and service are on the same WCF technology.

7)    NetPeerTcpBinding supports features as that of netTcpBinding but it provides secure binding for peer-to-peer environment with WCF Services.

Disconnected Queued Scenario:
8)    NetMsmqBinding is required in a cross machine environment with secure and reliable queued communication. This uses MSMQ as transport.


Be social and share with others via:


.NET Jobs from Indeed
Reader of this article might also be interested in:

By Imran Abdul Ghani
Software Developer (Senior)
United Arab Emirates
Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net & www.topwcftutorials.net.

Tuesday, July 17, 2012

WCF Service Opt-in Vs Opt-out

XmlSerializer was with Microsoft .NET since version 1.0 but with .NET framework version 3.0, Microsoft introduces DataContractSerializer.

DataContractSerializer is the default serializer for WCF although we can still use XmlSerializer for serialization in Windows Communication Foundation. A basic difference between the two serializers is the approach being used and that is the primary goal of this WCF tutorial.


DataContractSerializer uses Opt-In approach as opposite to XmlSerializer.

Opt-In approach says properties that are considered to be part of DataContract must be explicitly marked othewise will be ignore, while Opt-Out means all of the properties will be assumed to be part of the DataContract unless marked explicitly.


Be social and share with others via:


.NET Jobs from Indeed
Reader of this article might also be interested in:

Imran Abdul Ghani
Software Developer (Senior)
United Arab Emirates
Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net & www.topwcftutorials.net.

Tuesday, July 3, 2012

5 Simple Steps to Enable Transactions in WCF

Transaction is basically a logical unit of work comprising of activities that all needed to be succeeded or failed, and also it must be compliant with ACID principals.

Movement of money from a bank account to another is a simple example of a transaction. In this single transaction, two operations will be performed. One account will be debited (amount will be
taken from) and other will be credited (amount will be deposited).

Enabling transactions in Windows Communication Foundation is simple and straight forward but implementation sometimes becomes difficult depending upon the scenario. For example, implementing transactions in a distributed environment will definitely require effort and more things to consider.

Now, consider we already have developed a WCF service and we wanted to enable transactions on it. So, we will follow the steps below:

  1. Add System.Transactions namespace to WCF Service project.
  2. Set TransactionFlow property of the OperationContract attribute to Mandatory.
    Available options for TransactionFlow are:
    a. Mandatory - transaction must be flowed
    b. Allowed - transaction may be flowed
    c. Not Allowed - transaction is not flowed

    For example, our WCF service contract as follows:

    [TransactionFlow(TransactionFlowOptions.Mandatory]
    void MyMethod();

  3. Now, set the OperationBehavior attribute for the implementing method.

    [OperationBehavior(TransactionScopeRequired=true, TransactionAutoComplete=true)]
    void MyMethod()
    {
    }

    TransactionScopeRequired = true means it can only be called in a transaction.
    TransactionAutoComplete = true means that if the operation completes successfully, transaction will be committed.

  4. Enable Transactions for WCF Binding being used.
    For Example, In our configuration file bindings will be as follows:

    <bindings>
      <wsHttpBinding>
         <binding name=”httpBinding”  transactionFlow=”true” />
      </ wsHttpBinding >
    </bindings>

    Remember that we must choose a binding that supports transactions i.e. netTcpBinding, netNamedPipeBinding, wsHttpBinding, wsDualHttpBinding, and wsFederationHttpBinding.
  5. Need to start the transaction from client as:

    using System.Transaction;
    Using( var transScope = new TransactionScope())
    {   

         //Calling service methods
         IMyServiceClient client = new IMyServiceClient();
         client.MyMethod();
        
         transScope.complete();
       
    }

Optionally, If we wanted to specify the Isolation level for the transaction, we can add serviceBehavior attribute to implementing class as follows:

[ServiceBehavior(TransactionIsolationLevel=System.Transaction.IsolationLevel.Serializable)]
Public class MyService : IMyService{}

This is all we need to do for enabling transactions in WCF.
There are few more things regarding "Service Instancing and Sessions" that need to be considered while working with Transactions but this basic WCF tutorial is more focused on enabling transactions in simple scenario. In my later WCF article on Windows Communication Foundation Transactions on this blog, I'll discuss those concepts in more details.

Be social and share with others via:


.NET Jobs from Indeed
Reader of this article might also be interested in:

By Imran Abdul Ghani
Software Developer (Senior)
United Arab Emirates
Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net & www.topwcftutorials.net.