给定启用了事务流的事务感知绑定和具有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;
}
注意:
答案 0 :(得分:7)
您可以在TransactionScopeOption.Suppress
中使用TransactionScope
:
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress))
{
AuditServiceProxy.Write("Authentication failed for user " + username);
}
或将此抑制代码放入NonTransactionalLoggingService
方法调用