skip to Main Content

I’m using the Azure.Communication.PhoneNumbers SDK version 1.1.0 for C# to purchase phone numbers in Azure Communication Services (ACS). I’m following this guide which demonstrates how to perform the purchase operation and wait for result. This approach works, but the purchase operation can take between 50 seconds to over 2 minutes to complete, which is too long to block the client (Http request).

I’d like to make this operation asynchronous, so I can start the purchase operation and then periodically check the status until it completes, allowing the client call to finish once the purchase is started.

The PurchasePhoneNumbersOperation class has a HasCompleted property, which means I could use a loop to poll the status in a background. However, it would be more efficient to periodically check the status using the SDK client instead of background loop.

I noticed that the operation object has a GetRehydrationToken() method. My understanding is that I could potentially use this token to reconstruct the PurchasePhoneNumbersOperation object later and check the status. However, I couldn’t find clear documentation or examples on how to use the RehydrateAsync method, which expects an HttpPipeline.

My questions are:

  1. How can I properly use the GetRehydrationToken() and RehydrateAsync methods to check the status of the purchase operation asynchronously?
  2. Is there a better approach or recommended practice for tracking long-running operations like this in ACS?

Any guidance or examples would be greatly appreciated!

UPD: Looks like it’s possible to get the status of the operation via OperationId using REST Api. But it’s not supported by SDK.

2

Answers


  1. Chosen as BEST ANSWER

    After some investigation I have tested GetRehydrationToken() and for PurchasePhoneNumbersOperation it's not implemented and returns null. So, the only way to track operation status is to store the whole operation object in memory and call UpdateStatus() when needed.


  2. Firstly, begin with the phone number purchase operation and get the PurchasePhoneNumbersOperation object. Then Use the GetRehydrationToken() method to obtain a token that you can use to rehydrate the operation later. Store the token securely (e.g., in a database) by that you can retrieve it later to check the operation status.

    • Use the RehydrateAsync method with the rehydration token and an HttpPipeline to recreate the PurchasePhoneNumbersOperation object and check its status.

    Rehydrate the Operation and Check Status Asynchronously:

    using Azure.Communication.PhoneNumbers;
    using Azure.Core.Pipeline;
    using System;
    using System.Threading.Tasks;
    
    // Method to periodically check the status
    public async Task CheckPurchaseStatusAsync(string rehydrationToken)
    {
        var pipeline = HttpPipelineBuilder.Build(new PhoneNumbersClientOptions());
        var client = new PhoneNumbersClient("<your_connection_string>");
    
        // Rehydrate the operation
        PurchasePhoneNumbersOperation rehydratedOperation = PurchasePhoneNumbersOperation.RehydratePurchasePhoneNumbersOperation(rehydrationToken, pipeline);
    
        while (!rehydratedOperation.HasCompleted)
        {
            // Check the status (this will update the operation's state)
            await rehydratedOperation.UpdateStatusAsync();
    
            // Handle the status (e.g., log, notify, etc.)
            Console.WriteLine($"Status: {rehydratedOperation.Status}");
    
            // Wait before polling again (e.g., 10 seconds)
            await Task.Delay(TimeSpan.FromSeconds(10));
        }
    
        // Handle the final result
        if (rehydratedOperation.HasCompletedSuccessfully)
        {
            Console.WriteLine("Phone number purchase completed successfully.");
        }
        else
        {
            Console.WriteLine($"Phone number purchase failed with status: {rehydratedOperation.Status}");
        }
    }
    
    // Retrieve the token from storage and start checking the status
    string rehydrationToken = GetTokenFromDatabase();
    await CheckPurchaseStatusAsync(rehydrationToken);
    

    Purchase operation completed successfully:

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search