简体   繁体   English

如何在不等待 C# 的情况下取消分配 Azure 虚拟机

[英]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.DeallocateMicrosoft.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.

相关问题 如何从 C# 中的 PowerShell 脚本运行 Azure 虚拟机 - How to run Azure virtual machine from PowerShell script in C# 如何使用C#和Azure .Net SDK在Azure中克隆和管理虚拟机? - How to Clone and manage a virtual machine in Azure using C# and Azure .Net SDK? 使用C#代码或.net以编程方式在Windows Azure中创建虚拟机 - Create a virtual machine in windows azure programmatically with C# code or .net AZURE 得到虚拟机 ip SDK c# - AZURE get ip of virtual machine SDK c# 如何从C#代码更改Azure门户虚拟机规模集缩放实例数? - How to change azure portal virtual machine scale set scaling Instance count from c# code? 我需要有关如何在 azure 上制作虚拟机的建议,其中将安装实时 c# 编译器 - I need suggestion about How to make a virtual machine on azure in which will be installed live c# compiler C#使用Azure资源管理器(ARM)删除Azure虚拟机时获取结果状态 - C# Getting result status when deploing Azure Virtual Machine using Azure Resource Manager(ARM) 如何在C#编译器和虚拟机中处理lambda表达式? - How are lambda expressions treated in C# Compiler & Virtual Machine? 如何从虚拟机连接 C# 和 MySQL? - How can I connect C# with MySQL from Virtual Machine? 如何在c#中创建的c ++上释放对象 - How to deallocate object on c++ which is created on c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM