简体   繁体   English

ASP.net core 2. 0 get error when use transaction scope: The requested operation cannot be completed because the connection has been broken

[英]ASP.net core 2. 0 get error when use transaction scope : The requested operation cannot be completed because the connection has been broken

I have created an API to add multiple items.我创建了一个 API 来添加多个项目。 I use transaction scope because I don't have to insert all items if found a problem in any item.我使用事务 scope 因为如果在任何项目中发现问题,我不必插入所有项目。 so I have used TransactionScope so doing dispose of the transaction if found any validation failed in an item.所以我使用了 TransactionScope,如果发现项目中的任何验证失败,则处理该事务。 I use a loop of 100 items to avoid database time out, but in the loop, I got error like我使用 100 个项目的循环来避免数据库超时,但在循环中,我得到了类似的错误

"The requested operation cannot be completed because the connection has been broken". “由于连接中断,请求的操作无法完成”。

please let me know any solution tom fulfill my requirements请让我知道任何解决方案可以满足我的要求

I send XML to the database of each 100 items in a loop.我将 XML 循环发送到每 100 个项目的数据库。 code is a per below代码是下面的

using (TransactionScope transactionScope = new TransactionScope())
{
        try
        {
          for (int i = 0; i < objPricebook.ItembookMasterObject.Length; i = i + 100)
          {
             List<ItembookMasterObject> items = objPricebook.ItembookMasterObject.Skip(i).Take(100).ToList();
             string ItemXML = CreateXML(items); // CreateXML is function i have created to convert data in to XML
             if (ItemXML != "")
                 {
                   DS = obj.AddItemBook(ItemXML, objGuid.ToString());
                    if (DS.Tables.Count > 0)
                      {
                        if (DS.Tables[0].Rows.Count > 0)// this means any validation is failed. 
                          {
                            transactionScope.Dispose();
                            var id = obj.AddPriceBookLogError(DS.Tables[0]);
                             return Json(new { status = "Failed", message = "There is some problem with Item json });
                           }
                        }
                   }
            }



       }
      catch (Exception ex)
      {
      transactionScope.Dispose();
      return Json(new { message = "Item Json has some invalid imput", exceptionMessage = ex.Message, errorCode = "009" });
      }

     transactionScope.Complete();
     transactionScope.Dispose();
     return Json(new { status = "Success", message = "Price book is successfully added" });
}

Your current implementation is overkill.您当前的实施是矫枉过正。 using blocks always call Dispose() when execution leaves the block, regardless of how that happens.当执行离开块时, using块总是调用Dispose() ,不管它是如何发生的。 As such, you don't need to call Dispose() directly.因此,您不需要直接调用Dispose()

using (TransactionScope transactionScope = new TransactionScope())
{
    try
    {
        for (int i = 0; i < objPricebook.ItembookMasterObject.Length; i = i + 100)
        {
            List<ItembookMasterObject> items = objPricebook.ItembookMasterObject.Skip(i).Take(100).ToList();
            string ItemXML = CreateXML(items); // CreateXML is function i have created to convert data in to XML
            if (ItemXML != "")
            {
                DS = obj.AddItemBook(ItemXML, objGuid.ToString());
                if (DS.Tables.Count > 0)
                {
                    if (DS.Tables[0].Rows.Count > 0)// this means any validation is failed. 
                    {
                        var id = obj.AddPriceBookLogError(DS.Tables[0]);
                        return Json(new { status = "Failed", message = "There is some problem with Item json" });
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        return Json(new { message = "Item Json has some invalid imput", exceptionMessage = ex.Message, errorCode = "009" });
    }

    transactionScope.Complete();
    return Json(new { status = "Success", message = "Price book is successfully added" });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于对象已被垃圾回收,因此无法完成请求的操作 - The requested operation cannot be completed because the object has been garbage collected 由于DbContext已被处置错误,因此无法完成该操作 - The operation cannot be completed because the DbContext has been disposed error 操作无法完成,因为 DbContext 已在 mvc 中处置 - The operation cannot be completed because the DbContext has been disposed in mvc 该操作无法完成,因为已经丢弃了DbContext。” - operation cannot be completed because the DbContext has been disposed." 由于已处置DbContext,因此无法完成该操作 - The operation cannot be completed because the DbContext has been disposed EF6由于已经处理了DbContext,因此无法完成操作 - EF6 The operation cannot be completed because the DbContext has been disposed “由于DbContext已被处理”#2,因此无法完成操作 - “The operation cannot be completed because the DbContext has been disposed” #2 “由于已经处理了DbContext,因此无法完成操作” - “The operation cannot be completed because the DbContext has been disposed” “操作无法完成,因为 DbContext 已被释放。” - 'The operation cannot be completed because the DbContext has been disposed.' 由于已经处理了DbContext,因此无法完成操作 - The operation cannot be completed because the DbContext has been disposed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM