即使抛出异常,TransactionScope 也不会回滚

问题描述 投票:0回答:1

我在交易块内进行了一些 API 调用。据我所知,如果这些 API 中的任何一个抛出异常,那么事务应该回滚。通过回滚,我了解到成功执行的 API 所做的所有任务都被撤消。问题是,即使 API 抛出异常,scope.Complete() 也会到达并且不会发生回滚。

这是我的交易区块。调用它的方法没有 try catch 块

using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
    await UpdateAuditPlanL3Async(auditCalender);
    await SaveAuditManagementL3(auditMgmt);
    await SaveAuditManagementStdDetaiL3(mgmtStdDetails);
    await SaveAuditObservationDetailL3(obsdetail);
    scope.Complete();
}

等待方法的参数是列表

下面是一个示例代码,展示了我调用 API 的方式

public async Task UpdateAuditPlanL3Async(AuditCalendarL3 auditCalendarl3)
{
    string url = string.Concat(GlobalDeclaration.APIServerUrl, "UpdateAuditPlanL3?AuditplanId=" + auditCalendarl3.AuditplanId + "&Token=" + GlobalDeclaration.ApiToken);
    url = GlobalDeclaration.CleanURL(url);
    var myContent = JsonConvert.SerializeObject(auditCalendarl3);
    var requestContent = new StringContent(myContent, Encoding.UTF8, "application/json");
    var response = await Program.httpClient.PutAsync(url, requestContent);
    var responseCode = response.StatusCode;
    if (responseCode == HttpStatusCode.OK || responseCode == HttpStatusCode.Created || responseCode == HttpStatusCode.Accepted)
    {
        GlobalDeclaration.log.Info("UpdateAuditPlanL3 API executed successfully. The response code is " + responseCode);
    }
    else
    {
        GlobalDeclaration.exlog.Error("UpdateAuditPlanL3 API failed. The response code is " + responseCode);
        throw new Exception("UpdateAuditPlanL3 API execution failed.");
    }
}

在我的例子中,下面的 API 抛出了异常。现在我知道为什么抛出异常了。我想知道如何解决即使抛出异常也没有发生回滚的问题

private async Task SaveAuditObservationDetailL3(List<AuditObservationDetailL3> adobsdtl)
{
    string url = string.Concat(GlobalDeclaration.APIServerUrl, "SaveAuditObservationDetailL3?Token=" + GlobalDeclaration.ApiToken);
    url = GlobalDeclaration.CleanURL(url);
    var myContent = JsonConvert.SerializeObject(adobsdtl);
    var requestContent = new StringContent(myContent, Encoding.UTF8, "application/json");
    var response = await Program.httpClient.PostAsync(url, requestContent);
    var responseCode = response.StatusCode;
    if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.Accepted)
    {
        GlobalDeclaration.log.Info("SaveAuditObservationDetailL3 API executed successfully. The response code is " + responseCode);
        MessageBox.Show("Observation Checkpoints created successfully.");
    }
    else
    {
        GlobalDeclaration.exlog.Error("SaveAuditObservationDetailL3 API failed. The response code is " + responseCode);
        throw new Exception("SaveAuditObservationDetailL3 API failed. The response code is " + responseCode);
    }
}

我已经阅读了如何使用 transactionscope 以及它在各处提到应该发生回滚的内容。我还尝试在事务范围内使用 try catch 块,但无济于事。我什至删除了 TransactionScopeAsynFlowOption.Enabled 并尝试了 .Suppress,但即使抛出异常,它也始终达到 scope.Complete() 。

c# .net transactionscope
1个回答
0
投票

问题是,即使 API 抛出异常,scope.Complete() 也会达到......

不,我不认为这是这里发生的事情。一些更可能的选择:

  1. 没有抛出异常
  2. 异常被单独的方法吞没(捕获而不是重新抛出)
  3. 抛出异常并回滚范围,但您想要回滚的事物不支持环境事务(很少有事物支持)
  4. 抛出异常并回滚范围,而你想要回滚的东西确实支持环境事务,但用法不正确 - 例如,必须在环境事务中打开
    SqlConnection
    inside命令它支持这种用法
© www.soinside.com 2019 - 2024. All rights reserved.