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:
- How can I properly use the
GetRehydrationToken()
andRehydrateAsync
methods to check the status of the purchase operation asynchronously? - 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
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.Firstly, begin with the phone number purchase operation and get the
PurchasePhoneNumbersOperation
object. Then Use theGetRehydrationToken()
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.RehydrateAsync
method with the rehydration token and anHttpPipeline
to recreate thePurchasePhoneNumbersOperation
object and check its status.Rehydrate the Operation and Check Status Asynchronously:
Purchase operation completed successfully: