Monday, January 28, 2019

WCF Exception: “The service operation requires a transaction to be flowed”

WCF Exception: “The service operation requires a transaction to be flowed”
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:
  •  Complete() – all operations within the current transaction scope are completed successfully.
  •   Dispose () – it aborts the current transaction.

Example C# : 
        
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:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...