Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude an operation from a transaction

Tags:

c#

.net

wcf

Given a transaction-aware binding with transaction flow enabled and an operation Op1 that has TransactionFlowOption.Allowed, is it possible to make a different operation Op2 invoked from operation Op1 not participate in the transaction such that whatever operation Op2 does never rolls back in case something fails in operation Op1

Illustration

// Op1: LogOnUser
OperationBehavior(TransactionScopeRequired = true)]
public bool LogOnUser(String username, String password)
{
    // AuditWriteProxy declaration and instantiation
    var valid = false;

    /* Validation logic */

    // If validation failed
    if(!valid)
    {
        // Invoke an op in an Audit Service. 
         // Op2 = AuditService.Write
        // **MUST NOT BE ROLLED BACK EVEN AFTER WE [throw]**
       AuditServiceProxy.Write("Authentication failed for user " + username);

        throw new FaultException<AuthenticationFault>("Validation failed");
        // After throw, we expect everything transactional to rollback
    }

    AuditServiceProxy.Write("User " + username + " authenticated successfully");

    return true;
}

Notes:

  1. The AuditService.Write operation uses msmq binding and is one-way
  2. I tried TransactionFlowOption.NotAllowed on the the AuditService.Write operation contract as well TransactionScopeRequired=false on the implementation.
like image 263
John Gathogo Avatar asked Feb 22 '23 19:02

John Gathogo


1 Answers

You can use TransactionScopeOption.Suppress in a TransactionScope:

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress)) 
{ 
   AuditServiceProxy.Write("Authentication failed for user " + username); 
} 

or place this suppression code into a NonTransactionalLoggingService method call

like image 174
Mitch Wheat Avatar answered Feb 25 '23 08:02

Mitch Wheat