What are different Transaction Isolation Levels in WCF?

Here, the [Service Behavior] attribute specifies the Transaction Isolation level property. 

Transaction Isolation specifies the degree of isolation most compatible with other applications. 

The data affected by a transaction is called volatile.

There are 5 differenct types of Isolation levels in WCF, as follows-

1. READ UNCOMMITTED : - An uncommitted transaction can be read. This transaction can be rolled back later.

2. READ COMMITTED :- Will not read data of a transaction that has not been committed yet.

3. REPEATABLE READ : - Locks placed on all data and another transaction cannot read.

4. SERIALIZABLE :- Does not allow other transactions to insert or update data until the transaction is complete.

5. SNAPSHOT : - It raises error on modifying a data that has already been changed by any transaction.

What is Transaction in WCF ?

A transaction is a collection or group of one or more units of operation executed as a whole. Another way to say it is that transactions provide a way to logically group single pieces of work and execute them as a single unit, or transaction.

To implement transactions in WCF, we utilize TransactionScope class that automatically manages transactions and detect the scope of the transaction.

For example, if you are calling three services methods and third one fails to complete the operation, transaction will be rolled back unless it’s outside the boundary of a TransactionScope.

WCF supports transaction on following bindings:

1. WSHttpBinding
2. NetTcpBinding
3. NetNamedPipeBinding
4. WSDualHttpBinding
5. WSFederationHttpBinding


When you develop a Service Contract, you have to specify the TransactionFlow attribute on each
Operation Contracts that requires a Transaction to be handled.

There are following flow options in the TransactionFlow attribute.

  1. TransactionFlowOption.Allowed: Transaction can be flowed.
  2. TransactionFlowOption.Mandatory: Transaction must be flowed.
  3. TransactionFlowOption.NotAllowed: Transaction should not be flowed. This is default.

Now, implement the service and specify OperationBehavior attribute on each method. You can specify the TransactionScopeRequired = true / false.

Now enable the Transaction on the binding itself. Open your web.config file and specify the transactionflow = true

Now add service reference and consume the service. In this way you have implemented trasactions for your wcf services.

How to enable Transactions in WCF?


Follow below steps to enable transactions in WCF.

Step 1: Create Two WCF Services 
Step 2: Attribute Interface Methods with TransactionFlow 
Step 3: Attribute the Implementation with TransactionScopeRequired 
Step 4: Enable Transaction Flow using WCF Service Config File 
Step 5: Call the 2 Services in One Transaction and test if Transaction Works.

We will see all these steps in details in later posts.

Contracts in WCF

WCF defines 4 Types of Contracts:

1. Service Contract: Service Contract is an interface or a class that defines the service contract in a Windows Communication Foundation (WCF) application. A service contract is the gateway to a service for external applications to make use of the service functions, and at least one service contract should be available in a service.


2. Data Contract: Data Contract defines a type with a set of data members or fields that will be used as the composite type or data members in a service contract.


3. Message Contract: A Message Contract is used to control the structure of a message body and serialization process. It is used to send/access the information in the soap header. Using a Message Contract, we can customize the parameters which is sent using a SOAP message between the client and the server. The SOAP header is implemented in the namespace system.web.services.protocol.


4. Fault Contract: When any WCF service throws an exception, then the client cannot be notified directly of about exception. Because whenever an exception is thrown from a service, it cannot be sent to the client. So if we want to get actual exception which is occured in service functionality then a fault contract can be used as shown below.



What is WCF Endpoints

In WCF, we have Endpoints - Address, Binding and Contracts.

Address: WHERE ?
specifies where service is located or hosted
http://localhost:8080/MyWCFServices/GetDollarRate.svc
Binding: HOW ?
specifies how to communication will be done through the WCF Service and what type of communication should be with Client and Service.

It defines how the client will communicate with the service. There are different types of protocols are available for communicating Client to the service. Depend of different types of bindings communication happens.

Contracts: WHAT ?
specifies that the functionality of service, what will service do. We can use multiple contracts for the functionality of WCF service.

[ServiceContract]
public interface IService
{

[OperationContract]
string GetData(int value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: Add your service operations here
}


WCF defines 4 Types of Contracts:

  1. Service Contract
  2. Data Contract
  3. Message Contract
  4. Fault Contract

WCF Services- Bydefault Security for Bindings

This is very important thing regarding the security in WCF services.


Security in Windows Communication Foundation i.e. WCF can be divided at two levels-
  1. Transport Level Security
  2. Message Level Security

Some bindings are there with bydefault security as follows-


1. BasicHttpBinding

  • No Security
  • Interopable with ASMX Web Services

2. WsHttpBinding

  • Message Level Security
  • SOAP Messages are encrypted

3. NetTcpBinding

  • Transport Level Security
  • Packets are encrypted but not the messages (SOAP messages)

Difference between WCF and Web service

Here, you can find some differences between WCF and Webservices.

For serialization in a web service, we use System.Xml.serialization namespace.
WCF uses the System.Runtime.Serialization namespace for serialization.

In a web service, we need to add the [WebService] attribute to the class.
In WCF, we need to add the [ServiceContract] attribute to the class for defining the contracts.

We can host a web service in IIS.
We can host WCF in IIS, WAS (Windows Activation Service), self-hosting and a Windows Service.

Add the [WebMethod] attribute to the method in a web service.
Add the [OperationContract] attribute to the method in WCF.


Types of WCF Binding

There are mainly 8 types of bindings are available in WCF as below:


1. BasicHttpBinding

  • It is suitable for communicating with ASP.NET Web services (ASMX)-based services that comfort with WS-Basic Profile conformant Web services.
  • This binding uses HTTP as the transport and text/XML as the default message encoding.
  • Security is disabled by default
  • This binding does not support WS-* functionalities like WS- Addressing, WS-Security, WS-ReliableMessaging
  • It is fairly weak on interoperability.


2. WSHttpBinding

  • Defines a secure, reliable, interoperable binding suitable for non-duplex service contracts.
  • It offers lot more functionality in the area of interoperability.
  • It supports WS-* functionality and distributed transactions with reliable and secure sessions using SOAP security.
  • It uses HTTP and HTTPS transport for communication.
  • Reliable sessions are disabled by default.


3. WSDualHttpBinding


  • This binding is same as that of WSHttpBinding, except it supports duplex service. Duplex service is a service which uses duplex message pattern, which allows service to communicate with client via callback.
  • In WSDualHttpBinding reliable sessions are enabled by default. It also supports communication via SOAP intermediaries.


4. WSFederationHttpBinding


  • This binding support federated security. It helps implementing federation which is the ability to flow and share identities across multiple enterprises or trust domains for authentication and authorization. It supports WS-Federation protocol.


5. NetTcpBinding


  • This binding provides secure and reliable binding environment for .Net to .Net cross machine communication. 
  • By default it creates communication stack using WS-ReliableMessaging protocol for reliability, TCP for message delivery and windows security for message and authentication at run time. It uses TCP protocol and provides support for security, transaction and reliability.


6. NetNamedPipeBinding


  • This binding provides secure and reliable binding environment for on-machine cross process communication. It uses NamedPipe protocol and provides full support for SOAP security, transaction and reliability. 
  • By default it creates communication stack with WS-ReliableMessaging for reliability, transport security for transfer security, named pipes for message delivery and binary encoding.


7. NetMsmqBinding

  • This binding provides secure and reliable queued communication for cross-machine environment.
  • Queuing is provided by using MSMQ as transport.
  • It enables for disconnected operations, failure isolation and load leveling


8. NetPeerTcpBinding

  • This binding provides secure binding for peer-to-peer environment and network applications.
  • It uses TCP protocol for communication
  • It provides full support for SOAP security, transaction and reliability.

What are different Transaction Isolation Levels in WCF?

Here, the [Service Behavior] attribute specifies the Transaction Isolation level property.  Transaction Isolation specifies the degr...