[英]How to deallocate Azure Virtual Machine without waiting in C#
I am using the C# SDK for Microsoft Azure to stop (deallocate) a virtual machine.我正在使用 Microsoft Azure 的 C# SDK 来停止(解除分配)虚拟机。 I am trying to use either
Microsoft.Azure.Management.Compute.Fluent.IVirtualMachine.Deallocate
or Microsoft.Azure.Management.Compute.IVirtualMachine.DeallocateWithHttpMessagesAsync
.我正在尝试使用
Microsoft.Azure.Management.Compute.Fluent.IVirtualMachine.Deallocate
或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.我注意到 Azure CLI 文档中有一个
--no-wait
选项。
Source: https://docs.microsoft.com/en-us/cli/azure/vm?view=azure-cli-latest#az_vm_deallocate来源: 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?如何使用适用于 Azure 的 C# SDK 实现此目的?
I found my answer.我找到了我的答案。
I can use Microsoft.Azure.Management.Compute.IVirtualMachine.BeginDeallocateWithHttpMessagesAsync
to initiate the deallocation process.我可以使用
Microsoft.Azure.Management.Compute.IVirtualMachine.BeginDeallocateWithHttpMessagesAsync
来启动解除分配过程。 The method returns immediately without waiting for the VM to actually finish the deallocation process.该方法立即返回,无需等待 VM 实际完成解除分配过程。
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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.