WCF Exception: “The service operation requires a transaction
to be flowed”
Example C# :
Recently I encountered the wcf transaction error while
calling to WCF service and this WCF service is transaction based service and I tried
to call service without any transaction scope.
try
{
Account.AccountServiceClient
account = new Account.AccountServiceClient();
account.UpdateTaxRefundAmount(taxId, refund);
}
catch (Exception ex)
{
throw;
}
In this blog, we will
discuss how to create transaction scope and call transaction enabled service
method.
You can use TranscationScope class to define a block of code
which will be executed in a transaction and this call expose below methods to
manage the current transaction status:
- Dispose () – it aborts the current transaction.
using
(TransactionScope ts = new
TransactionScope(TransactionScopeOption.RequiresNew))
{
try
{
// Call service method
Account.AccountServiceClient
account = new Account.AccountServiceClient();
account.UpdateTaxRefundAmount(taxId, refund);
ts.Complete();
}
catch (Exception ex)
{
ts.Dispose();
}
}
you can define the behavior of truncation scope by passing
the below transcationScopeOption to constructor of transcationScope class
- Required - a transaction is required by the scope, if there is already an existing transaction scope otherwise it creates new transaction.
- RequiredNew – it always create a new transaction.
- Surpress – it suppress the parent transaction or surrounding transaction.
No comments:
Post a Comment