从事务中排除操作

时间:2011-10-05 07:39:00

标签: c# .net 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;
}

注意:

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

1 个答案:

答案 0 :(得分:7)

您可以在TransactionScopeOption.Suppress中使用TransactionScope

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

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