给定启用事务流的事务感知绑定(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;
}
注意事项:
- AuditService.Write 操作使用 msmq 绑定(bind)并且是单向的
- 我在 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/