【问题标题】:How to get success Response from Microsoft.Azure.Management.Fluent api method如何从 Microsoft.Azure.Management.Fluent api 方法获得成功响应
【发布时间】:2018-12-05 22:49:36
【问题描述】:

我已经通过 c# 中的 Fluent API 类在 Azure 门户上创建了虚拟机。 我的 C# 代码是:

public JsonResult createVM()
{
    try
    {
        IAzure azure = Authenticate(subscriptionId);
        azure.VirtualMachines.Define(vmName)
             .WithRegion(location)
             .WithExistingResourceGroup(ResourceGroupName)
             .WithExistingPrimaryNetworkInterface(networkInterface)
             .WithSpecializedOSDisk(managedDisk, operatingSystem)
             .WithSize(vmSize)
             .Create();

        //Here i want complete response from above code..if success.
        return Json(string.Empty);
    }
    catch (CloudException ex)
    {
        Response.Write(ex.Response.Content);
        return Json(string.Empty);
    }
}

如果执行失败,我会在 catch 块中得到响应。 但如果执行成功,我们需要响应。

【问题讨论】:

    标签: c# azure virtual-machine azure-sdk-.net azure-management-api


    【解决方案1】:

    将 Azure VM 创建代码段分配给 var。检查该 var 是否不为空。如果是,则 VM 已成功创建。如果出现异常,您显然会转到 catch 块。如果您愿意,您可以验证新创建的 VM 对象的任何属性,就像他们在 unit test 中所做的那样。

    【讨论】:

    • 是的,我们可以做到。但我想要完整的 httpresponse 文本。我们如何实现这一点。
    • 它返回一个对象本身。如果您具体了解 http 响应,那么您可以使用 azure ARM rest api PUT management.azure.com/subscriptions{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}?api-version=2017-12- 01
    【解决方案2】:

    由于您使用 Azure 管理库创建 Azure VM,SDK 已自动将所有成功响应转换为 IVirtualMachine,您只需访问 IVirtualMachine 实例即可检索所需的所有属性,而不是直接访问原始 HTTP 响应。

    您可以按照here 下的PrintVirtualMachine(IVirtualMachine virtualMachine) 检索您期望的属性并构造一个包含您的VM 属性的新匿名类,然后将其返回给您的客户端,如下所示:

    return Json(new
    {
        ComputerName = linuxVM.ComputerName,
        PowerState = linuxVM.PowerState,
        ProvisioningState = linuxVM.ProvisioningState
        .
        .
    });
    

    我不明白您为什么想要原始 HTTP 响应。但是,如果您仍然坚持只检索纯 HTTP 响应,则需要按照 Aravind 评论的建议,自行明确发送具有相关授权的 REST API Virtual Machines - Create Or Update。对于身份验证,您可以按照 Authentication API to access subscriptions 注册您的 AAD 应用程序以访问 https://management.azure.com/ 以创建您的 Azure VM。此时,您需要自己做所有事情,并且可以控制这个过程。

    【讨论】:

      【解决方案3】:

      我能够跟踪 Microsoft.Azure.Management.Fluent 类的日志。 关注Follow this link- Log and Trace section

      我在数据库中有日志响应:

       /// <summary>
          /// Here we can handle response.insert response in database
          /// </summary>
          /// <param name="invocationId">The invocation identifier.</param>
          /// <param name="response">The response message instance.</param>
          public void ReceiveResponse(string invocationId, HttpResponseMessage response)
          {
                   logapResponse(response);
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-25
        • 2022-11-23
        • 2017-08-06
        • 1970-01-01
        • 2020-05-08
        • 2017-11-28
        相关资源
        最近更新 更多