how to rollback the transaction of API from Azure function which calls the API

Vadivu s Dharani 0 Reputation points
2024-12-03T12:43:50.82+00:00

how to rollback the transaction of API from Azure function which calls the API

below is my code in azure function. when My Azure function fails in any case I want to rollback the API changes

using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { try { // Your existing Azure Function code here. // Call the API var response = await CallApiAsync(receivedData); if (!response.IsSuccessStatusCode) { throw new Exception("API call failed."); } int x = 0; int y = 5; int z = 5 / 0; // This will throw an exception scope.Complete(); } catch (Exception ex) { _logger.LogError("An exception occurred: {exception}", ex); // The transaction will be rolled back automatically if scope.Complete() is not called. } }

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
653 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,250 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Khadeer Ali 1,400 Reputation points Microsoft Vendor
    2024-12-04T03:38:30.18+00:00

    Hi Vadivu s Dharani,

    Welcome to the Microsoft Q&A Platform! Thank you for reaching out regarding your query on rollback the transaction of API from Azure function.

    By looking at the code shared, you are using TransactionScope inside Azure Function and calling the API.

    You want to roll back the transaction of API from Azure function which is not possible directly by using TransactionScope because to handle transaction rollbacks in an Azure Function, you can use the TransactionScope class, as you've done in your code. However, it's important to note that TransactionScope works well with databases that support distributed transactions, but it might not directly roll back changes made by an external API.

    To achieve rollback behavior when calling an external API, you need to implement compensating actions manually.

    Here is the updated code you might consider:

    
    using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
    {
        try
        {
            // Your existing Azure Function code here.
            // Call the API
            var response = await CallApiAsync(receivedData);
            if (!response.IsSuccessStatusCode)
            {
                throw new Exception("API call failed.");
            }
    
            // Simulate an error
            int x = 0;
            int y = 5;
            int z = 5 / 0; // This will throw an exception
    
            scope.Complete();
        }
        catch (Exception ex)
        {
            _logger.LogError("An exception occurred: {exception}", ex);
    
            // Call a compensating transaction to undo the API changes
            await CallApiUndoAsync(receivedData);
        }
    }
    
    
    

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.


  2. Bruce (SqlWork.com) 68,486 Reputation points
    2024-12-05T18:12:31.96+00:00

    as suggested, TransactionScope only works with databases that implement 2 phase commit.

    with azure functions, the Saga pattern is a better approach:

    https://learn.microsoft.com/en-us/azure/architecture/reference-architectures/saga/saga

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.