从事务中排除操作

Joh*_*ogo 3 .net c# wcf

给定启用事务流的事务感知绑定和具有TransactionFlowOption.Allowed的操作Op1,是否可以使从操作Op1调用的不同操作Op2不参与事务,使得任何操作Op2永远不会回滚以防万一操作失败Op1

插图

// 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;
}
Run Code Online (Sandbox Code Playgroud)

笔记:

  1. AuditService.Write操作使用msmq绑定并且是单向的
  2. 我在AuditService.Write操作契约上尝试了TransactionFlowOption.NotAllowed,在实现上尝试了TransactionScopeRequired = false.

Mit*_*eat 7

您可以 TransactionScopeOption.SuppressTransactionScope:

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

或将此抑制代码放入NonTransactionalLoggingService 方法调用中