TranscationScope具有多个数据库调用

时间:2016-01-14 07:23:37

标签: c# entity-framework transactionscope

我的方案是将发票明细(PDF格式)发送到客户的paynow按钮点击事件的emailId。

我尝试过以下操作但在调用

后获得异常
actionPDF.BuildPdf(ControllerContext)

例外是

  

超时已过期。操作完成之前经过的超时时间或服务器没有响应

代码:

dbDetails _db = new dbDetails();

[HttpPost]
public JsonResult Add(Model mdl)
{
    using (TransactionScope _ts = new TransactionScope())
    {
        //Insertion logic of invoice goes here
        ...
        ...

        int i = _db.SaveChanges();

        // if successfull insertion
        if(i > 0)
        {
            var actionPDF = new Rotativa.ActionAsPdf("GetPdfReceipt", new { RegId = _receiptDetails.StudentRegistrationID.Value })
            {
               FileName = "Receipt.pdf"
            };

            // Dynamic student receipt pdf
            ***Getting exception here****
            var byteArrayDynamic = actionPDF.BuildPdf(ControllerContext);

            // Mail sending logic
            ......
            ......

            _ts.Complete();   
        }
    }
} 

public ActionResult GetPdfReceipt(int RegId)
{
     Common _cmn = new Common();
     var _studentRegistration = _db.StudentRegistrations
                                 .AsEnumerable()
                                 .Where(r => r.Id == RegId)
                                 .FirstOrDefault();

    var _mdlReceiptPdf = new ReceiptPdfVM
    {
         CentreCode = _studentRegistration.StudentWalkInn.CenterCode.CentreCode,
         CompanyAddress = _studentRegistration.StudentWalkInn.CenterCode.Address,
         CompanyPhoneNo = _studentRegistration.StudentWalkInn.CenterCode.PhoneNo,
         CourseFee = _studentRegistration.TotalCourseFee.Value,
         CourseTitle = string.Join(",", _studentRegistration.StudentRegistrationCourses
                                              .Select(rc => rc.MultiCourse.CourseSubTitle.Name)),
         CROName = _studentRegistration.StudentWalkInn.CROCount == (int)EnumClass.CROCount.ONE ? _studentRegistration.StudentWalkInn.Employee1.Name :
                                                                                                  _studentRegistration.StudentWalkInn.Employee1.Name + ',' + _studentRegistration.StudentWalkInn.Employee2.Name,
         Duration = _studentRegistration.TotalDuration.Value,
         ReceiptDate = _studentRegistration.StudentReceipts.Last(sr => sr.Status == true).DueDate.Value.ToString("dd/MM/yyyy"),
         ReceiptNo = _studentRegistration.StudentReceipts.Last(sr => sr.Status == true).ReceiptNo,
         RegistrationNumber = _studentRegistration.RegistrationNumber,
         ServiceTax = _studentRegistration.TotalSTAmount.Value,
         StudentMaskedEmailId = _cmn.MaskString(_studentRegistration.StudentWalkInn.EmailId, "email"),
         StudentMaskedMobileNo = _cmn.MaskString(_studentRegistration.StudentWalkInn.MobileNo, "mobile"),
         StudentName = _studentRegistration.StudentWalkInn.CandidateName,
         ServiceTaxRegistrationNo = _studentRegistration.StudentWalkInn.CenterCode.STRegNo,
         TotalAmount = _studentRegistration.TotalAmount.Value,
         TotalAmountInWords = _cmn.NumbersToWords(_studentRegistration.TotalAmount.Value).ToUpper(),
         TotalCourseFeePaid = _studentRegistration.StudentReceipts
                                   .Where(r => r.Status == true)
                                   .Sum(r => r.Fee.Value),
         ManagerName = _cmn.GetManager(_studentRegistration.StudentWalkInn.CenterCodeId.Value)
                              .Name,
         ReceiptDetailsList = _db.StudentReceipts
                                       .AsEnumerable()
                                       .Where(rc => rc.StudentRegistrationID == RegId)
                                       .Select(rc => new ReceiptPdfVM.ReceiptDetails
                                       {
                                           CourseFee = rc.Fee.Value,
                                           DatePaid = rc.DueDate.Value.ToString("dd/MM/yyyy"),
                                           ReceiptNo = rc.ReceiptNo
                                       }).ToList()
    }

    return View("Receipts", _mdlReceiptPdf);
}

protected override void Dispose(bool disposing)
{
        _db.Dispose();
        base.Dispose(disposing);
}

如何解决这个问题?任何帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

我认为问题在于,在生成PDF文档时,您的待处理数据库事务会超时。在生成PDF之前将_ts.Complete移动到。

dbDetails _db = new dbDetails();

[HttpPost]
public JsonResult Add(Model mdl)
{
    using (TransactionScope _ts = new TransactionScope())
    {
        //Insertion logic of invoice goes here
        ...
        ...
        int i= _db.SaveChanges();
        //if successfull insertion
        if(i>0)
        {
            _ts.Complete();  
            var actionPDF = new Rotativa.ActionAsPdf("GetPdfReceipt", new { RegId = _receiptDetails.StudentRegistrationID.Value })
            {
               FileName = "Receipt.pdf"
            };
            //Dynamic student receipt pdf
            ***Getting exception here****
            var byteArrayDynamic = actionPDF.BuildPdf(ControllerContext);

           //Mail sending logic
           ......
           ......



        }


    }

}