In WCF 4.0, How can I commit 3 different service operation as a single Transaction? (Commit in SOA)
I have 3 different WCF service like below, Each service method invokes DB operation
service1.CreateEmployee();
service2.SendSetupRequestForEmployee();
service3.GiveOfficePermissionToEmployee();
Even if one operation fails entire thing should be rolled back...any help appreciated.
The short answer: Make your service calls under a TransactionScope, and make sure the calls themselves are set up to run under transactions.
TLDR read this article here.
Basically, you need to decorate your Operation Contract method as such:
[TransactionFlow(TransactionFlowOption.Allowed)]
void MyWcfServiceCall() {...}
and the service method call itself with:
[OperationBehavior(TransactionScopeRequired = true)]
void MyWcfServiceCall() {...}
and call under a TransactionScope
using (TransactionScope tx = new TransactionScope(TransactionScopeOption.RequiresNew)) {
myServiceClient.MyWcfServiceCall();
myOtherServiceClient.MyOtherWcfServiceCall();
tx.Complete();
}
in your config file for the bindings, set transactionFlow to true:
<bindings>
<wsHttpBinding>
<binding name="MyServiceBinding" transactionFlow="true" ... />
</wsHttpBinding>
</bindings>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With