Transactions provide a way to group a set of actions or operations into a
single indivisible unit of execution. A transaction is a collection of
operations with the following properties(this is taken from MSDN):
- Atomicity. This ensures that either all of the updates
completed under a specific transaction are committed and made durable or they
are all aborted and rolled back to their previous state.
- Consistency. This guarantees that the changes made under a
transaction represent a transformation from one consistent state to another. For
example, a transaction that transfers money from a checking account to a savings
account does not change the amount of money in the overall bank account.
- Isolation. This prevents a transaction from observing
uncommitted changes belonging to other concurrent transactions. Isolation
provides an abstraction of concurrency while ensuring one transaction cannot
have an unexpected impact on the execution of another transaction.
- Durability. This means that once committed, updates to managed resources (such as a database record) will be persistent in the face of failures.
We can understand by it following Example:
Step(1) Create WCF service and Add Attribute Interface Method with TransactionFlow as [TransactionFlow(TransactionFlowOption.Allowed)]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WCFTransaction
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
void InsertEmpDetails();
}
}
Step(2) Implements Interface with attribute TransactionScopeRequired as [OperationBehavior(TransactionScopeRequired = true)]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WCFTransaction
{
public class Service1 : IService1
{
[OperationBehavior(TransactionScopeRequired = true)]
public void InsertEmpDetails()
{
// Insrt code will come here
}
}
}
Step(3)Create Service client and consume it.
Step(4)Enable Transaction flow using WCF Service Config File
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WCFTransaction" transactionFlow="true" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:1480/Service1.svc" binding="wsHttpBinding"
bindingConfiguration="WCFTransaction" contract="WCFTransaction.IService1"
name="WSHttpBinding_IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
Step(5) Now calling two objects of the service in one transaction
For transaction we need to add System.Transactions namespace.
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
{
try
{
WCFTransaction.Service1Client obj = new WCFTest.WCFTransaction.Service1Client();
obj.InsertEmpDetails();
WCFTransaction.Service1Client obj2 = new WCFTest.WCFTransaction.Service1Client();
obj2.InsertEmpDetails();
ts.Complete();
}
catch (Exception ex)
{
ts.Dispose();
}
}
So in the above manner we can enable transaction in WCF service.
Transaction in WCF
ReplyDelete