Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Cannot access a disposed object #8462


User avatar
0
CNS created

There is a problem when we are running different transactions and jumping from one appservice to the other but some how following error arises with in the transactions: : 'Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'ERPDbContext'.' Please explain


12 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team
    • What is your product version?
    • What is your product type (Angular or MVC)?
    • What is product framework type (.net framework or .net core)?

    Please share some code.

  • User Avatar
    0
    CNS created

    .net core and angular

  • User Avatar
    0
    maliming created
    Support Team

    hi @CNS

    Please share some related code. :)

  • User Avatar
    0
    CNS created

    1 Step :

        private async void ProcessReceiptTransfer(VoucherPostingDto input)
        {
            var receiptHeader = _porecHeaderRepository.GetAll().Where(o => o.TenantId == AbpSession.TenantId
            && o.DocNo >= input.FromDoc && o.DocNo <= input.ToDoc && o.Posted == false).ToList();
    
            foreach (var item in receiptHeader)
            {
                CreateOrEditPORECHeaderDto receipt = new CreateOrEditPORECHeaderDto()
                {
                    Id = item.Id
                };
                await _receiptEntryAppService.ProcessReceiptEntry(receipt);
            }
        }
        
        
    

    2 Step:

        public async Task<string> ProcessReceiptEntry(CreateOrEditPORECHeaderDto input)
        {
            var alertMsg = "";
            var currency = _voucherEntryAppService.GetBaseCurrency();
            var user = _userRepository.GetAll().Where(o => o.Id == AbpSession.UserId).SingleOrDefault().UserName;
            var transBook = _icSetupRepository.GetAll().Where(o => o.TenantId == AbpSession.TenantId).SingleOrDefault().TRBookID;
            var receiptHeader = _porecHeaderRepository.FirstOrDefault(o => o.TenantId == AbpSession.TenantId && o.Id == input.Id);
            var receiptDetail = _porecDetailRepository.GetAll().Where(o => o.TenantId == AbpSession.TenantId && o.DetID == input.Id);
            var icItem = _itemRepository.GetAll().Where(o => o.TenantId == AbpSession.TenantId);
            string narration = receiptHeader.Narration;
            var additionalData = _icrecaExpRepository.GetAll().Where(o => o.TenantId == AbpSession.TenantId && o.LocID == input.LocID && o.DocNo == input.DocNo);
    
            List<CreateOrEditGLTRDetailDto> gltrdetailsList = new List<CreateOrEditGLTRDetailDto>();
    
            var transferDetailList = from o in receiptDetail
                                     join i in icItem on new { A = o.ItemID, B = o.TenantId } equals new { A = i.ItemId, B = i.TenantId }
                                     group o by new { i.Seg1Id } into gd
                                     select new PORECDetailDto
                                     {
                                         Amount = gd.Sum(x => x.Amount),
                                         ItemID = gd.Key.Seg1Id,
                                     };
    
            foreach (var item in transferDetailList)
            {
                var caID = _inventoryGlLinkRepository.GetAll().Where(o => o.TenantId == AbpSession.TenantId && o.LocID == receiptHeader.LocID && o.SegID == item.ItemID).FirstOrDefault().AccRec;
                var daID = _inventoryGlLinkRepository.GetAll().Where(o => o.TenantId == AbpSession.TenantId && o.LocID == receiptHeader.LocID && o.SegID == item.ItemID).FirstOrDefault().AccRec;
                //Credit Amount
                gltrdetailsList.Add(new CreateOrEditGLTRDetailDto
                {
                    Amount = -Convert.ToDouble(item.Amount),
                    AccountID = caID,
                    Narration = narration,
                    SubAccID = 0,
                    LocId = receiptHeader.LocID,
                    IsAuto = true
                });
    
                //Debit Amount
                gltrdetailsList.Add(new CreateOrEditGLTRDetailDto
                {
                    Amount = Convert.ToDouble(item.Amount),
                    AccountID = daID,
                    Narration = narration,
                    SubAccID = 0,
                    LocId = Convert.ToInt32(receiptHeader.LocID),
                    IsAuto = false
                });
            }
    
            foreach (var item in additionalData)
            {
                //Debit Amount
                gltrdetailsList.Add(new CreateOrEditGLTRDetailDto
                {
                    Amount = Convert.ToDouble(item.Amount),
                    AccountID = item.AccountID,
                    Narration = narration,
                    SubAccID = 0,
                    LocId = Convert.ToInt32(item.LocID),
                    IsAuto = false
                });
            }
    
            VoucherEntryDto autoEntry = new VoucherEntryDto()
            {
                GLTRHeader = new CreateOrEditGLTRHeaderDto
                {
                    BookID = transBook,
                    NARRATION = narration,
                    DocDate = Convert.ToDateTime(receiptHeader.DocDate),
                    DocMonth = Convert.ToDateTime(receiptHeader.DocDate).Month,
                    Approved = true,
                    AprovedBy = user,
                    AprovedDate = DateTime.Now,
                    Posted = false,
                    LocId = receiptHeader.LocID,
                    CreatedBy = user,
                    CreatedOn = DateTime.Now,
                    AuditUser = user,
                    AuditTime = DateTime.Now,
                    CURID = currency.Id,
                    CURRATE = currency.CurrRate,
                    ConfigID = 0
                },
                GLTRDetail = gltrdetailsList
            };
    
            if (autoEntry.GLTRDetail.Count() > 0 && autoEntry.GLTRHeader != null)
            {
                var voucher = await _voucherEntryAppService.ProcessVoucherEntry(autoEntry);
                receiptHeader.Posted = true;
                //receiptHeader.PostedBy = user;
                // receiptHeader.PostedDate = DateTime.Now;
                receiptHeader.LinkDetID = voucher[0].Id;
                var transh = await _porecHeaderRepository.FirstOrDefaultAsync((int)receiptHeader.Id);
                ObjectMapper.Map(receiptHeader, transh);
    
                alertMsg = "Save";
            }
            else
            {
                alertMsg = "NoRecord";
            }
            return alertMsg;
        }
        
        
    

    3 Step:

        public async Task<List<GLTRHeaderDto>> ProcessVoucherEntry(VoucherEntryDto input)
        {
            var gltrHeader = ObjectMapper.Map<GLTRHeader>(input.GLTRHeader);
    
            if (AbpSession.TenantId != null)
            {
                gltrHeader.TenantId = (int)AbpSession.TenantId;
            }
    
            gltrHeader.DocNo = GetMaxDocId(input.GLTRHeader.BookID);
            var getGenratedId = await _gltrHeaderRepository.InsertAndGetIdAsync(gltrHeader);
    
    
            foreach (var item in input.GLTRDetail)
            {
    
                var gltrDetail = ObjectMapper.Map<GLTRDetail>(item);
    
                if (AbpSession.TenantId != null)
                {
                    gltrDetail.TenantId = (int)AbpSession.TenantId;
    
                }
                gltrDetail.LocId = input.GLTRHeader.LocId;
                gltrDetail.DetID = getGenratedId;
                await _gltrDetailRepository.InsertAsync(gltrDetail);
            }
    
            List<GLTRHeaderDto> returnList = new List<GLTRHeaderDto>();
            returnList.Add(new GLTRHeaderDto
            {
                Id=getGenratedId,
                DocNo = gltrHeader.DocNo,
                LocId=gltrHeader.LocId
            });
    
            return returnList;
        }
    
        
    
  • User Avatar
    0
    maliming created
    Support Team

    Don't use void as the return type of an asynchronous method.

    private async void ProcessReceiptTransfer(VoucherPostingDto input) should be private async Task ProcessReceiptTransfer(VoucherPostingDto input)

    Please share your abp version and complete exception information (including other logs before and after the error.)

  • User Avatar
    0
    CNS created

    where to know about the abp version

  • User Avatar
    0
    maliming created
    Support Team

    where to know about the abp version

    And

    Don't use void as the return type of an asynchronous method.
    
    private async void ProcessReceiptTransfer(VoucherPostingDto input) should be private async Task ProcessReceiptTransfer(VoucherPostingDto input)
    
    Please share complete exception information (including other logs before and after the error.)
    
  • User Avatar
    0
    CNS created

    System.ObjectDisposedException: 'Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'ERPDbContext'.'

    Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code).

    this error arises normally when 2 time loop executed

  • User Avatar
    0
    maliming created
    Support Team

    hi @CNS

    Can you use Zero's demo project to create a simple project that reproduces the problem? Use the simplest code to reproduce the problem.

    Please share the project with me: [email protected]

  • User Avatar
    0
    CNS created

    all other services are working fine but on this service we are facing problem. I already shared whole problematic code with you.

  • User Avatar
    0
    maliming created
    Support Team

    It would be best if you could share a simple demo project for me. : )

  • User Avatar
    0
    aaron created
    Support Team

    @CNS

    Don't use void as the return type of an asynchronous method.

    private async void ProcessReceiptTransfer(VoucherPostingDto input) should be private async Task ProcessReceiptTransfer(VoucherPostingDto input)

    1. Have you made the change above?
    2. Can you share code for the caller of ProcessReceiptTransfer method?