Open Closed

Azure client send message to service bus and update entity with IRepository in appservice transaction. #6404


User avatar
0
mariovh created
  • ABP Framework version: v6.0.0
  • UI Type: Blazor WASM
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes/no
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

Hello, i have the scenario where i want to send a message to azure service bus topic and after that set the entity as processed, wich is update a property as true. I want to do it transactional in app service method. So if database update throws exception, message should not be published in azure service bus preserving events integrity.

Wrapping in Transaction Scope throws this exception when updating in database:

I can't update because IRepository update is transactional, so message is not published in topic because is wrapped in the same transaction scope as database update, this is correct.

If I configure the module with:

making the update operation non transactional, both operations works fine, but is not the behavior i want because my database operations to be not transactional.

My question is, that if I remove transaction scope and AbpUnitOfWorkDefaultOptions transaction behavior is Enabled by default, asuming that in every appservice method begins a transactional UOW, when the update operation throws an exception, the azure client send message is publishing the message in the topic wich is wrong behavior. Both operations, send message and then update entity are in the same appservice method, same UOW, same Transaction Scope, so if an exception is thrown, a rollback of all operations should be made, and this is not the case.

I dont know what is the correct way to implement a pattern like this using Appservices UOW, and making transactional both operations, send message to service bus and update an entity via generic repository.

Thanks!


1 Answer(s)
  • User Avatar
    0
    Anjali_Musmade created
    Support Team Support Team Member

    Hello ,

    Please check this link https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-transactions https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample06_Transactions.md

    Try to implement this way.

    ``using var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
    try
    {
        await _donationRepository.UpdateAsync(donation, cancellationToken: stoppingToken);  // Database update first
        await _sender!.Send(topic: "donations", donationProto, properties, stoppingToken);  // Message publishing after successful update
        ts.Complete();
    }
    catch (Exception ex)
    {
        ts.Dispose();
        // Handle exception appropriately, potentially using more specific exception types
        throw new UserFriendlyException(ex.Message);
    }``
    

    Thanks,

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11