Friday, 14 February 2014

Jquery tutorial for beginners

Simple Steps to Enable Transactions in WCF

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:
    1. Mandatory - transaction must be flowed
    2. Allowed - transaction may be flowed
    3. 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{}

WCF Interview Questions

This Top WCF Tutorials post has most important and basic questions that are asked during a WCF interview on hot topics with detailed answers and explanation.


1. What is the difference between WCF and ASMX Web Services?

Simple and basic difference is that ASMX or ASP.NET web service is designed to send and receive messages using SOAP over HTTP only. While WCF can exchange messages using any format (SOAP is default) over any transport protocol (HTTP, TCP/IP, MSMQ, NamedPipes etc).

Another tutorial WCF Vs ASMX has detailed discussion on it.


2. What are WCF Service Endpoints? Explain.

For Windows Communication Foundation services to be consumed, it’s necessary that it must be exposed; Clients need information about service to communicate with it. This is where service endpoints play their role.

WCF service endpoint has three basic elements i.e. Address, Binding and Contract.

Address: It defines “WHERE”. Address is the URL that identifies the location of the service.
Binding: It defines “HOW”. Binding defines how the service can be accessed.
Contract: It defines “WHAT”. Contract identifies what is exposed by the service.


3. What are the possible ways of hosting a WCF service? Explain.

For a Windows Communication Foundation service to host, we need at least a managed process, a ServiceHost instance and an Endpoint configured. Possible approaches for hosting a service are:

      1.    Hosting in a Managed Application/ Self Hosting
             a.    Console Application
             b.    Windows Application
             c.    Windows Service
        2.    Hosting on Web Server
             a.    IIS 6.0 (ASP.NET Application supports only HTTP)
             b.    Windows Process Activation Service (WAS) i.e. IIS 7.0 supports HTTP, TCP,
                    NamedPipes, MSMQ.


4. How we can achieve Operation Overloading while exposing WCF Services?

By default, WSDL doesn’t support operation overloading. Overloading behavior can be achieved by using “Name” property of OperationContract attribute.

[ServiceContract]
interface IMyCalculator
{
   [OperationContract(Name = "SumInt")]
   int Sum(int arg1,int arg2);


   [OperationContract(Name = "SumDouble")]
   double Sum(double arg1,double arg2);
}

When the proxy will be generated for these operations, it will have 2 methods with different names i.e. SumInt and SumDouble.


5. What Message Exchange Patterns (MEPs) supported by WCF? Explain each of them briefly.

       1.     Request/Response
       2.    One Way
       3.    Duplex

Request/Response
It’s the default pattern. In this pattern, a response message will always be generated to consumer when the operation is called, even with the void return type. In this scenario, response will have empty SOAP body.
One Way
In some cases, we are interested to send a message to service in order to execute certain business functionality but not interested in receiving anything back. OneWay MEP will work in such scenarios.
If we want queued message delivery, OneWay is the only available option.
Duplex
The Duplex MEP is basically a two-way message channel. In some cases, we want to send a message to service to initiate some longer-running processing and require a notification back from service in order to confirm that the requested process has been completed.


6. What is DataContractSerializer and How its different from XmlSerializer?

Serialization is the process of converting an object instance to a portable and transferable format. So, whenever we are talking about web services, serialization is very important.

Windows Communication Foundation has DataContractSerializer that is new in .NET 3.0 and uses opt-in approach as compared to XmlSerializer that uses opt-out. Opt-in means specify whatever we want to serialize while Opt-out means you don’t have to specify each and every property to serialize, specify only those you don’t want to serialize.
DataContractSerializer is about 10% faster than XmlSerializer but it has almost no control over how the object will be serialized. If we wanted to have more control over how object should be serialized that XmlSerializer is a better choice.


7. How we can use MessageContract partially with DataContract for a service operation in WCF?

MessageContract must be used all or none. If we are using MessageContract into an operation signature, then we must use MessageContract as the only parameter type and as the return type of the operation.


8. Which standard binding could be used for a service that was designed to replace an existing ASMX web service?


The basicHttpBinding standard binding is designed to expose a service as if it is an ASMX/ASP.NET web service. This will enable us to support existing clients as applications are upgrade to WCF.


9. Please explain briefly different Instance Modes in WCF?
WCF will bind an incoming message request to a particular service instance, so the available modes are:

Per Call: instance created for each call, most efficient in term of memory but need to maintain session.

Per Session: Instance created for a complete session of a user. Session is maintained.

Single: Only one instance created for all clients/users and shared among all.Least efficient in terms of memory.

Please follow "3 techniques for Instance Management in WCF".

10. Please explain different modes of security in WCF? Or Explain the difference between Transport and Message Level Security.

In Windows Communication Foundation, we can configure to use security at different levels

a.    Transport Level security means providing security at the transport layer itself. When dealing with security at Transport level, we are concerned about integrity, privacy and authentication of message as it travels along the physical wire. It depends on the binding being used that how WCF makes it secure because most of the bindings have built-in security.
                     
             <netTcpBinding>
            <binding name=”netTcpTransportBinding”>
               <security mode=”Transport”>
                          <Transport clientCredentialType=”Windows” />
               </security>
             </binding>
             </netTcpBinding>

b.    Message Level Security
For Tranport level security, we actually ensure the transport that is being used should be secured but in message level security, we actually secure the message. We encrypt the message before transporting it. 

             <wsHttpBinding>
             <binding name=”wsHttpMessageBinding”>
               <security mode=”Message”>
                           <Message clientCredentialType=”UserName” />
               </security>
              </binding>
             </wsHttpBinding>

It totally depends upon the requirements but we can use a mixed security mode also as follows:

             <basicHttpBinding>
             <binding name=”basicHttp”>
               <security mode=”TransportWithMessageCredential”>
                          <Transport />
                               <Message clientCredentialType=”UserName” />
               </security>
             </binding>
             </basicHttpBinding>

Detail Tutorial on Understanding Transactions and Creating Transaction Enabled WCF Services

Tutorial on Understanding Transactions and Creating Transaction Enabled WCF Services.

Introduction

In this article we will discuss about creating transaction enabled WCF service. We will see what needs to be done on the WCF service end so that it support transactions. We will also see how a client application can work with these transaction enabled services using a sample application. 

Background

When we talk about transactions in database operations, we need to perform some operation(database operations) in such a way that either all the operations are successful or all of them fail. This would result in the amount information being same once the transaction is successful or it fails.

Properties of Transaction

By definition a transaction must be Atomic, Consistent, Isolated and Durable. What does we mean by all these terms
·         Atomic: Atomic means that all the statements (SQL statement or operations) that are a part of the transaction should work as atomic operation i.e. either all are successful or all should fail.
·         Consistent: This means that in case the atomic transaction success, the database should be in a state that reflect changes. If the transaction fails then database should be exactly like it was when the transaction started.
·         Isolated: If more than one transactions are in process then each of these transactions should work independently and should not effect the other transactions.
·         Durable: Durability means that once the transaction is committed, the changes should be permanent i.e. these changes will get saved in database and should persist no matter what(like power failure or something).
Now having transactions from a simple class library's perspective ot from a simple .Net applications, it is just a matter of calling the operations within a transaction, checking whether all the operations are successful or not and deciding whether to commit or rollback the transaction. Refer this article to know about transactions in normal application: A Beginner's Tutorial for Understanding Transactions and TransactionScope in ADO.NET[^]
But from a WCF service perspective, since the service itself may be running on a remote server and all the communication between the service and the client is in form of messages, the service itself need some configuration so that it can be made transaction enabled.
Now in rest of the article we will see how we can configure a WCF service to support transactions and how we can call a WCF operation within transactions.

Using the code

Understanding Two Phase Commit

The WCF transactions happen using two phase commit protocol. Two phase commit protocol is the protocol that is used to enable transactions in a distributed environment. This protocol mainly consist of two phases:
·         Prepare phase: In this phase the client application performs the operations of a WCF service. WCF service determines whether the requested operation will be successful or not and notify the client about the same.
·         Commit Phase: In the commit phase the client checks for the responses it got from the prepare phase and if all the responses are indicating that the operation can be carried out successfully the transaction is committed. If the response from any one of the operations indicates failure then the transaction will be rolled back. The actual operation on the service end will happen in the commit phase.
Now from the protocol, it is pretty clear that the WCF service will have to send the notification of whether the operation will succeed or fail to the client application. It would mean that the One way operations can never support transactions. The operations that support transactions have to follow the Request-Response Model(refer this for details on message exchange modes: Tutorial on Message Exchange Patterns and Asynchronous Operations in WCF[^])

A note on binding

Since services communicate in form of messages the underlying message specifications play a very important role in supporting transactions. To have the possibility of transactions, the WS-AT(WS-AtomicTransaction) protocol need to be used. The binding that supports this is wsHttpBinding. So we will be using this binding to create our transaction enabled service.

Description of the Test Application

To illustrate the above process, let say I have two account holders, one person is trying to transfer some money to other person. From the database perspective this operation consist of two sub-operations i.e.
·         Debiting the first account by specified amount.
·         Secondly, crediting the second account with required amount.
Now from a technical perspective, if the first operation is successful but second one fails the result would be that the first persons account will be debited but second one will not be credited i.e. we loose the amount of information. The other way round will in fact increase the amount ion second account without even debiting the first amount.
So the bottom-line here is that we need either both of them to be successful to both of them should fail. Success of any one operation will result in inconsistent results and thus even if one operation fails we need to rollback what we did in the other operation. This is precisely where transaction are useful.
Let us say that the operations for credit and debit are exposed separately from a service. What we need to do is that, we need to transaction enable this service and then call these two methods within a transaction. The transaction will be committed only when both the operation indicate success. In case any one operation indicates failure or throws an exception, the transaction will not be committed.

Creating the Service

Let us create a simple service with a ServiceContract that exposes operations to debit an account, credit an account and get the account balance information for any account.
http://www.codeproject.com/images/minus.gif Collapse | Copy Code
[ServiceContract]
public interface IService1
{
    [OperationContract]
    bool PerformCreditTransaction(string creditAccountID, double amount);
 
    [OperationContract]
    bool PerformDebitTransaction(string debitAccountID, double amount);
 
    [OperationContract]
    decimal GetAccountDetails(int id);
}
Now we want the operations PerformCreditTransaction and PerformDebitTransaction to work within a transaction. To do this we need to make some configuration in the service. The very first thing that is required with the OperationContract is to set the TransactionFlow property with the desired TransactionFlowOption. There are 3 possible values for TransactionFlowOption:
·         Mandatory: This specifies that this function can only be called within a transaction.
·         Allowed: This specifies that this operation can be called within a transaction but its not mandatory.
·         NotAllowed: This specifies that this operation can not be called within a transaction.
Now for both our operations, we want the operations to be called mandatory within a transaction so we will specify them with this option.
http://www.codeproject.com/images/minus.gif Collapse | Copy Code
[ServiceContract]
public interface IService1
{
    [OperationContract, TransactionFlow(TransactionFlowOption.Mandatory)]
    bool PerformCreditTransaction(string creditAccountID, double amount);
 
    [OperationContract, TransactionFlow(TransactionFlowOption.Mandatory)]
    bool PerformDebitTransaction(string debitAccountID, double amount);
 
    [OperationContract]
    decimal GetAccountDetails(int id);
}
Now to illustrate the implementation part,
We will work on a small application that contains a single table database. This table contains the account id and the amount present in the account.
The sample DB table looks like:
http://www.codeproject.com/KB/WCF/570915/table.jpg
The UI will look like:
http://www.codeproject.com/KB/WCF/570915/Screenshot.jpg
And now to perform these operations, we will write simple ADO.NET code in our service implementation. The important thing from the service implementation perspective is that the service implementation also needs to be decorated/adorned with OperationBehavior attribute with TransactionScopeRequired property set to true. So now let us look at the sample implementation of the service operations.
http://www.codeproject.com/images/minus.gif Collapse | Copy Code
public class Service1 : IService1
{
    readonly string CONNECTION_STRING = ConfigurationManager.ConnectionStrings["SampleDbConnectionString1"].ConnectionString;
 
    [OperationBehavior(TransactionScopeRequired = true)]
    public bool PerformCreditTransaction(string creditAccountID, double amount)
    {
        bool creditResult = false;
 
        try
        {
            using (SqlConnection con = new SqlConnection(CONNECTION_STRING))
            {
                con.Open();
 
                // And now do a credit
                using (SqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = string.Format(
                        "update Account set Amount = Amount + {0} where ID = {1}",
                        amount, creditAccountID);
 
                    // Let us emulate some failure here to see the that transaction will not
                    // get committed
                    // return false;
 
                    creditResult = cmd.ExecuteNonQuery() == 1;
                }
            }
        }
        catch
        {
            throw new FaultException("Something went wring during credit");
        }
        return creditResult;
    }
 
    [OperationBehavior(TransactionScopeRequired = true)]
    public bool PerformDebitTransaction(string debitAccountID, double amount)
    {
        bool debitResult = false;
 
        try
        {
            using (SqlConnection con = new SqlConnection(CONNECTION_STRING))
            {
                con.Open();
 
                // Let us do a debit
                using (SqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = string.Format(
                        "update Account set Amount = Amount - {0} where ID = {1}",
                        amount, debitAccountID);
 
                    debitResult = cmd.ExecuteNonQuery() == 1;
                }
            }
        }
        catch
        {
            throw new FaultException("Something went wring during debit");
        }
        return debitResult;
    }
 
    public decimal GetAccountDetails(int id)
    {
        decimal? result = null;
 
        using (SqlConnection con = new SqlConnection(CONNECTION_STRING))
        {
            using (SqlCommand cmd = con.CreateCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = string.Format("select Amount from Account where ID = {0}", id);
 
                try
                {
                    con.Open();
                    result = cmd.ExecuteScalar() as decimal?;
                }
                catch (Exception ex)
                {
                    throw new FaultException(ex.Message);
                }
            }
        }
 
        if (result.HasValue)
        {
            return result.Value;
        }
        else
        {
            throw new FaultException("Unable to retrieve the amount");
        }
    }
}
Note: The code is written to elaborate the transaction enabled services only, it is not as per the coding standards i.e it is vulnerable to SQL injection. It should not be taken as code that could go in production. It is just the sample code and has a lot of scope for improvement.
Now we have our ServiceContract ready and service implementation ready. Let us now use the appropriate binding i.e. wsHttpBinding to use this service. Also, in the service configuration we need to specify that this service is transaction enabled. This can be done by setting the transactionFlow property of the binding configuration ofwsHttpBiding to true
Note: Please refer the web.config of the sample code to see how this is done.

Test Application

From our test application, we will simply call the functions within a transaction(using TransactionScope). We will check both the operations' return value, if both of the indicates success, we will commit the transaction(by calling Complete method on TransactionScope object). If any operation fails we will rollback the transaction by not calling Complete function and simply letting the TransactionScope object go out of scope).
http://www.codeproject.com/images/minus.gif Collapse | Copy Code
private void PerformTransaction(string creditAccountID, string debitAccountID, double amount)
{
    // they will be used to decide whether to commit or rollback the transaction
    bool debitResult = false;
    bool creditResult = false;
 
    try
    {
        using (TransactionScope ts = new TransactionScope())
        {
            using (ServiceReference1.Service1Client client = new ServiceReference1.Service1Client())
            {
                debitResult = client.PerformDebitTransaction(debitAccountID, amount);
                creditResult = client.PerformCreditTransaction(creditAccountID, amount);
            }
 
            if (debitResult && creditResult)
            {
                // To commit the transaction 
                ts.Complete();
            }
        }
    }
    catch
    {
        // the transaction scope will take care of rolling back
    }
}
The code currently will work fine i.e. both the methods will return true and the transaction will get committed. To check the rollback action, we have a simple return false; statement commented in our service'sPerformCreditTransaction operation, un-comment this statement to check that the transaction will not get committed.
Note: The code snippets shows the relevant code in the context. Please look at the sample code to get the full understanding.
So now we have a service that supports transactions. Before wrapping up let is look at the main operations we need in order to make a service transaction enabled. 
1.       Decorate the OperationContract with required TransactionFlowOption.
2.       Decorate the operation implementation with OperationBehavior with TransactionScopeRequired as true.
3.       Use wsHttpBinding to utilize the underlying WS-AtomicTransaction protocol.
4.       Specify the transactionFlow property of the binding configuration of wsHttpBinding as true.