Explaining the meaning of transaction and 'transaction isolation' is silly so I am skipping it. To be able to write transactional webservices is cool! How about writing services that can 'flow' transactions?
A flowed transaction is a situation in which a transaction id(say from database) is passed over the wire and used on the receiving side(WCF) to continue executing with in the same scope.
Let us learn about the attributes and properties that support transactions first and later see how they work together.
a. Operation(interface/service)
[OperationContract]
[TransactionFlow(TransactionFlowOption.Required)]
Other options are Allowed and Not Allowed.
b. Behavior (class/service level)
TransactionIsolationLevel=IsolationLevel.ReadCommitted,
TransactionTimeout="00:00:30")]
By default serializable transactions are created.
c. Operation (class/service level)
[OperationBehaviour(TransactionScopeRequired=true,TransactionAutoComplete=true)]
Defines transaction behaviour with in service.
Tip: A service instance is recycled by default based on the InstanceContextMode setting (eg:PerCall = recycle after every call). But if you want to use a fresh instance after the transaction is completed use
ReleaseInstanceMode eg:-
[OperationBehavior(
ReleaseInstanceMode=ReleaseInstanceMode.AfterCall, TransactionAutoComplete=true
)]
public string SampleMethod(string msg)
{
}
{
//1st service
//second service
TransactionOptions transactionOptions = new TransactionOptions ();
transactionOptions.IsolationLevel = IsolationLevel.ReadCommitted;
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew,transactionOptions))
{
}
Transaction Batching
The relatively high cost of creating a transaction and committing it in every receive operation can be avoided with Transaction Batching service behavior.
<transactedBatching maxBatchSize="Integer" />
Exam Tip:
Service Behavior:
Operation Contract:
Let us learn about the attributes and properties that support transactions first and later see how they work together.
a. Operation(interface/service)
[OperationContract]
[TransactionFlow(TransactionFlowOption.Required)]
Other options are Allowed and Not Allowed.
b. Behavior (class/service level)
- TransactionAutoCompleteOnSessionClose: Specifies whether pending transactions are completed when the current session closes.
- TransactionIsolationLevel: Determines the isolation level of the transaction.
- TransactionTimeout: Specifies the period in which a transaction has to complete.
TransactionIsolationLevel=IsolationLevel.ReadCommitted,
TransactionTimeout="00:00:30")]
By default serializable transactions are created.
[OperationBehaviour(TransactionScopeRequired=true,TransactionAutoComplete=true)]
Defines transaction behaviour with in service.
Tip: A service instance is recycled by default based on the InstanceContextMode setting (eg:PerCall = recycle after every call). But if you want to use a fresh instance after the transaction is completed use
ReleaseInstanceMode eg:-
[OperationBehavior(
ReleaseInstanceMode=ReleaseInstanceMode.AfterCall, TransactionAutoComplete=true
)]
public string SampleMethod(string msg)
{
}
d.Client side Call
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew)){
try
{
//1st service
ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
obj.UpdateData();
ServiceReference2.Service1Client obj1 = new ServiceReference2.Service1Client();
obj1.UpdateData();
ts.Complete();
}
catch (Exception ex)
{
ts.Dispose();
}
}
The code can also be written like this:TransactionOptions transactionOptions = new TransactionOptions ();
transactionOptions.IsolationLevel = IsolationLevel.ReadCommitted;
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew,transactionOptions))
{
}
Transaction Batching
The relatively high cost of creating a transaction and committing it in every receive operation can be avoided with Transaction Batching service behavior.
<transactedBatching maxBatchSize="Integer" />
Exam Tip:
Service Behavior:
- TransactionAutoCompleteOnSessionClose
- TransactionIsolationLevel
- TransactionTimeout
- transactedBatching
Operation Contract:
- TransactionFlow
- TransactionScopeRequired
- TransactionAutoComplete
No comments:
Post a Comment