如何在不等待C#的情况下释放Azure虚拟机

时间:2018-02-26 01:33:52

标签: c# azure azure-virtual-machine

我正在使用C#SDK for Microsoft Azure来停止(解除分配)虚拟机。我正在尝试使用module top( input wire [(84*50)-1:0] input_data, // TODO make I/O ports ); genvar i; for (i=0; i<84; i=i+1) begin : amp TEST u_test ( .in_input ( input_data[i*50 +:50] ) ); end endmodule Microsoft.Azure.Management.Compute.Fluent.IVirtualMachine.Deallocate。两者似乎都在等待虚拟机完成重新分配过程。

我想在不阻塞的情况下解除分配虚拟机,等待解除分配完成。

我在Azure CLI文档中注意到有Microsoft.Azure.Management.Compute.IVirtualMachine.DeallocateWithHttpMessagesAsync选项。

来源:https://docs.microsoft.com/en-us/cli/azure/vm?view=azure-cli-latest#az_vm_deallocate

如何使用C#SDK for Azure实现此目的?

2 个答案:

答案 0 :(得分:4)

我找到了答案。

我可以使用Microsoft.Azure.Management.Compute.IVirtualMachine.BeginDeallocateWithHttpMessagesAsync来启动释放过程。该方法立即返回,无需等待VM实际完成重新分配过程。

答案 1 :(得分:1)

麻烦找出如何使用已接受的答案,因此想共享代码段。

using Microsoft.Azure.Management.Compute.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;


....


async Task DeallocateAzureVirtualMachine()
{
    // there are other ways to obtain credentials as well. 
    // this one is related to an App registration
    var credentials = SdkContext.AzureCredentialsFactory
                    .FromServicePrincipal("clientId", "secretKey", "tenantId",
                    AzureEnvironment.AzureGlobalCloud);

    var restClient = RestClient
                        .Configure()
                        .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
                        .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                        .WithCredentials(credentials)
                        .Build();

    using (var computeManagementClient = new ComputeManagementClient(restClient))
    {
        await computeManagementClient.VirtualMachines
            .BeginDeallocateWithHttpMessagesAsync("resource-group-name", "vm-name");
    }
}