UnitOfWork:如何回滚和保留日志

时间:2017-11-14 08:08:05

标签: unit-of-work transactionscope aspnetboilerplate

我有一个方法如下:

using (var unitOfWork = _unitOfWorkManager.Begin())
{
    var result = await ExcuteOperator(input);
    if (!result.WorkflowStatus) 
    {
        unitOfWork.Dispose();
    }
    unitOfWork.Complete();
}


public async Task<ExecuteOperatorOutput> ExcuteOperator(ExecuteOperatorInput input)
{                  
    //Doing something
    DoSomething(input);

    //Insert log
    await _logRepository.InsertAsync(logInput);

    //Recursive
    return await ExcuteOperator(input);
}

我的问题是:如何在unitOfWork之前手动回滚unitOfWork.Complete()而不抛出异常并保留插入的日志?

1 个答案:

答案 0 :(得分:2)

无法手动回滚unitOfWork

查看理由/尝试:https://github.com/aspnetboilerplate/aspnetboilerplate/issues?q=rollback

尽管如此,欢迎有兴趣的读者尝试并解决问题。

  

如何在事务回滚后保留日志?

开始一个新的unitOfWork完成:

public async Task<ExecuteOperatorOutput> ExcuteOperator(ExecuteOperatorInput input)
{                  
    //...

    using (var unitOfWork = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
    {
        await _logRepository.InsertAsync(logInput);
        unitOfWork.Complete();
    }

    //...
}