Wednesday, 22 November 2017

WCF Vs ASMX Web services

WCF Vs ASMX Web services
Simple and basic difference is that ASMX web service is designed to send and receive messages using SOAP over HTTP only. While WCF service can exchange messages using any format (SOAP is Default) over any protocol (HTTP, TCP/IP, MSMQ, Named Pipes etc).
ASMX is simple but limited in many ways as compared to WCF:
  1. ASMX web service can be hosted only in IIS, while WCF service has all the following hosting options:
    1. IIS
    2. WAS (Window Process Activation Services)
    3. WCF provided Host (Self Hosting)
    4. Console Application
    5. Window NT services
    6. ASMX web services support is limited to HTTP while WCF supports HTTP, TCP, MSMQ, and Named Pipes.
    7. ASMX Security is limited. Normally Authentication (Objects) and Authorization (Class or classes, we can say subject) is done using IIS and ASP.NET Security Configuration and Transport Layer Security. For message Layer Security, WSE can be used.
WCF provide a consistent security programming model for any protocol and it supports many of the same capabilities as IIS and WS-* Security protocols, additionally, it provides support for claim-based Authorization that provides finer-grained control over resources than role-based security. WCF security is consistent regardless of the host that is used to implement WCF service.
  1. Another major different between is that ASMX web services uses XmlSerializer for Serialization while WCF uses DataContractSerializer which is far better in performance than XmlSerializer .
Key Issues with XmlSerializer in serializing .NET types to XML are:
  1. Only public fields or properties of the  .NET types can be translated to xml.
  2. Only the classes that implement IEnumerable can be translated.
  3. Classes the implement IDectionary, such as Hashtable cannot be serialized.





WCF Service - Simple Steps to Enable Transactions
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();
  1. 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.
  1. 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” /></binding>
      </ wsHttpBinding >
    </bindings>

    Remember that we must choose a binding that supports transactions i.e. netTcpBinding, netNamedPipeBinding, wsHttpBinding, wsDualHttpBinding, and wsFederationHttpBinding.
  1. 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.

No comments:

Post a Comment