c# - 从事务中排除操作

标签 c# .net wcf

给定启用事务流的事务感知绑定(bind)和具有 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 绑定(bind)并且是单向的
  2. 我在 AuditService.Write 操作契约(Contract)上尝试了 TransactionFlowOption.NotAllowed,在实现上尝试了 TransactionScopeRequired=false。

最佳答案

您可以在 TransactionScope 中使用 TransactionScopeOption.Suppress:

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

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

关于c# - 从事务中排除操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7658128/

相关文章:

c# - 为什么无法获取已启动进程的主窗口句柄?

jquery - 如何使用 MVC 和 WCF 进行客户端验证而不重复逻辑?

c# - 路径动画

c# - 如何通过安装驱动程序来检查Windows是否繁忙?

c# - C# 中的线程

.net - WPF DocumentPaginator 和 DocumentPage 意外裁剪

c# - 简易喷油器 : Factory classes that need to create classes with dependencies

WCF 全局 (.asax) 行为

c# - 通过 WCF 抛出异常的正确方法

javascript - 如何在发布 html 表单时进行 Base64 编码?