Open Closed

Error on UnitOfWork in exception #7216


User avatar
0
andmattia created
  • ABP Framework version: v7.4.5
  • UI Type: Angular
  • Database System: PostgreSQL
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes

I've an handler to sync data from microservices

 public async Task HandleEventAsync(ProductEto eventData)
    {
        if (eventData != null)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin())
            {
                _currentTenant.Change(eventData.TenantId);
                try
                {
                    var gmd = _objectMapper.Map<ProductEto, ProductSync>(eventData);

                    await _productSyncRepository.InsertAsync(gmd);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, "Handle Event ProductEto Error");
                }
                finally
                {
                    await unitOfWork.CompleteAsync();
                }
            }
        }
        else
        {
            Logger.LogWarning("Input data is null into handle event ProductEto");
        }
    }

In this case I've a PK duplicate so Postgres raise an excetion but this happen in finally and my handler continue to retry process my data.

Also I observe that if I call InsertAsyc/UpdateAsync and not return to a local varibile update or insert in some case does't work, if I return the value it always work.

Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can try the code below

    
    public async Task HandleEventAsync(ProductEto eventData)
    {
        if (eventData != null)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin(requiresNew: true))
            {
                _currentTenant.Change(eventData.TenantId);
                try
                {
                    var gmd = _objectMapper.Map<ProductEto, ProductSync>(eventData);
    
                    await _productSyncRepository.InsertAsync(gmd);
                    await unitOfWork.CompleteAsync();
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, "Handle Event ProductEto Error");
                }
            }
        }
        else
        {
            Logger.LogWarning("Input data is null into handle event ProductEto");
        }
    }
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    andmattia created

    Hi

    I'll try but I think it works beacuse task raise exception inside the try but is this the best approch to use UOW in using?

    About Insert/Update issue any idea?

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    If there is an unhandled exception the uow should fall back/fail.

    The InsertAsyc/UpdateAsync method has an autoSave parameter. You can set it to true.

    https://docs.abp.io/en/abp/latest/Unit-Of-Work#alternative-to-the-savechanges

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 28, 2026, 11:44
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.