【问题标题】:C# Getting result status when deploing Azure Virtual Machine using Azure Resource Manager(ARM)使用 Azure 资源管理器(ARM)部署 Azure 虚拟机时 C# 获取结果状态
【发布时间】:2016-12-12 05:36:55
【问题描述】:

我想使用带有 .NET C# 的 Azure 资源管理器 (ARM) 获得 azure vm 部署的结果,以识别其成功或失败。

我找到了以下示例。

https://docs.microsoft.com/en-us/azure/virtual-machines/virtual-machines-windows-csharp-template

本文在部署时使用了“return await”语句。

public static async Task<DeploymentExtended> CreateTemplateDeploymentAsync(
  TokenCredentials credential,
  string groupName,
  string deploymentName,
  string subscriptionId){
Console.WriteLine("Creating the template deployment...");
var deployment = new Deployment();
deployment.Properties = new DeploymentProperties

{
 Mode = DeploymentMode.Incremental,
 Template = File.ReadAllText("..\\..\\VirtualMachineTemplate.json"),
 Parameters = File.ReadAllText("..\\..\\Parameters.json")
};
var resourceManagementClient = new ResourceManagementClient(credential) 
  { SubscriptionId = subscriptionId };
 return await resourceManagementClient.Deployments.CreateOrUpdateAsync(
 groupName,
 deploymentName,
 deployment);
 }

我该如何处理结果? 我想根据结果来划分程序。

【问题讨论】:

    标签: c# azure azure-resource-manager


    【解决方案1】:

    我们可以使用Properties.ProvisioningState 获取部署状态。但是当它部署VM失败时,可能会抛出异常,所以我们需要用代码捕获异常。

    1.代码演示:

     var token = GetAccessTokenAsync();
     var credential = new TokenCredentials(token.Result.AccessToken);
     string provisoningStatus = "Failed";
     try
       {
         var result =CreateTemplateDeploymentAsync(credential, "tom", "MyWindowsVM", "you subscription Id")
                       .Result;
                    provisoningStatus = result.Properties.ProvisioningState;
       }
     catch (Exception)
      {
    
         //ToDo
      }
      if (provisoningStatus.Equals("Failed"))
      {
             //TODo
      }
    
    }
    
    1. 创建虚拟机成功

    1. 从 Azure 门户检查

    如果没有捕获异常就失败了

    【讨论】:

    • 您的建议非常好理解。我会尽快查看并回复
    • 嗨,汤姆。我还有一个问题。 “成功”是否意味着部署完全完成并运行虚拟机?我怀疑它只是说 Azure 正确获得了一个 rest API 并且将从现在开始部署的消息。
    猜你喜欢
    • 1970-01-01
    • 2015-11-20
    • 2018-01-29
    • 2022-11-30
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多