You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Azure SDK ArmClient通过ID查询长时运行操作状态?

通过操作ID查询Azure SDK中ArmClient长时运行操作状态

核心实现方案

Azure SDK的ArmClient提供了GetArmOperationAsync方法,可直接通过操作ID重新获取长时运行操作的实例,进而检查状态或获取结果。


步骤1:保存初始操作ID

发起操作时,先提取并持久化操作ID(比如存入数据库或缓存),供后续查询使用:

ArmOperation<SqlDatabaseResource> operation = await sqlServerResource.GetSqlDatabases()
                        .CreateOrUpdateAsync(WaitUntil.Started, newDatabaseName, backupData);
// 保存操作ID到持久化存储
string operationId = operation.Id;

步骤2:实现通过ID查询状态的方法

在新的GET接口或查询方法中,用ArmClient重新获取操作实例并检查状态:

public async Task<IActionResult> CheckOperationStatus(string operationId)
{
    // 初始化ArmClient(需与发起操作时使用相同的凭据配置)
    var armClient = new ArmClient(new DefaultAzureCredential());
    
    // 通过操作ID重新获取ArmOperation实例,指定目标资源类型
    var operation = await armClient.GetArmOperationAsync<SqlDatabaseResource>(operationId);
    
    // 检查操作核心状态
    bool isCompleted = operation.HasCompleted;
    bool isSucceeded = operation.HasSucceeded;
    
    // 操作完成且成功时,可获取最终的数据库资源
    SqlDatabaseResource? targetDatabase = null;
    if (isCompleted && isSucceeded)
    {
        targetDatabase = await operation.WaitForCompletionAsync();
    }
    
    // 返回自定义状态响应
    return Ok(new 
    {
        OperationId = operationId,
        IsCompleted = isCompleted,
        IsSucceeded = isSucceeded,
        TargetDatabaseId = targetDatabase?.Id
    });
}

关键注意事项

  • 确保ArmClient的凭据配置与发起操作时一致,否则会因权限问题无法查询状态。
  • 操作ID是Azure资源管理器标准URI,格式类似/subscriptions/{订阅ID}/resourceGroups/{资源组}/providers/Microsoft.Sql/servers/{服务器}/databases/{数据库}/operations/{操作ID}。
  • 若操作完成时间过久,Azure可能清理操作记录,此时查询会返回404,需提前处理该场景。

内容的提问来源于stack exchange,提问作者Musealali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:57:16