1
votes

I am using the C# SDK for Microsoft Azure to stop (deallocate) a virtual machine. I am trying to use either Microsoft.Azure.Management.Compute.Fluent.IVirtualMachine.Deallocate or Microsoft.Azure.Management.Compute.IVirtualMachine.DeallocateWithHttpMessagesAsync. Both seem to wait for the virtual machine to complete the deallocation process.

I want to deallocate virtual machines without blocking to wait for the deallocate to complete.

I notice in the Azure CLI documentation that there is a --no-wait option.

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

How can I achieve this using the C# SDK for Azure?

2
If I am not mistaken, I think the --no-wait option means the command should terminate immediately after sending the request and not wait for the operation to finish. - Gaurav Mantri
Same thought with @GauravMantri. I think with the Async method of deallocation, you don't need to wait as the async says it all. The cancellationToken property would be the same functionality with --no-wait - EagleDev
@GauravMantri Yes I know. That's the behaviour that I want from the C# SDK. - Matt Houser
@ThuanNg Even the Async version will wait. It responds back with the Task which allows me to proceed, however, the task itself is still waiting for the VM to complete. That leaves a thread/task waiting for the VM to complete. I don't even want that. - Matt Houser
Understood. Perhaps by design it would prevent you from executing further function against in-use virtual machine (in case it is still being deallocated). Also just read from Microsoft doc that DeallocateWithHttpMessagesAsync is used for Scale Set, not a specific vritual machine. With VM Scale Set, of course it is a set of VMs which requires a wait. - EagleDev

2 Answers

5
votes

I found my answer.

I can use Microsoft.Azure.Management.Compute.IVirtualMachine.BeginDeallocateWithHttpMessagesAsync to initiate the deallocation process. The method returns immediately without waiting for the VM to actually finish the deallocation process.

1
votes

Had troubles finding out how to use accepted answer, so wanted to share the snippet.

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");
    }
}