从 Microsoft.Azure.Management.Fluent 迁移到 Azure.ResourceManager 时如何处理 IAzure Interface?

问题描述 投票:0回答:2

我正在迁移源代码以使用 Azure.ResourceManager 包,而不是 Microsoft.Azure.Management.Fluent(已弃用)。 我的代码使用 IAzure 接口,如下所述:

/// <summary>
/// The azure context.
/// </summary>
private readonly IAzure azureContext;

this.azureContext = this.GetAzureForSubAsync().ConfigureAwait(false).GetAwaiter().GetResult();

// method declaration here for GetAzureForSubAsync
private async Task<IAzure> GetAzureForSubAsync()
{
    // <some logic>
    return Azure.Configure()
        .WithLogLevel(HttpLoggingDelegatingHandler.Level.BodyAndHeaders)
        .Authenticate(new AzureCredentials(tokenCredentials, tokenCredentials, tenantId, AzureEnvironment.AzureGlobalCloud))
        .WithSubscription(this.subscriptionId);
}

我不确定哪个接口会在新包中为我提供等效的逻辑,即。 Azure.ResourceManager

在构建管道时,我收到此错误:

##[error]src\Test\LoadTest\ARMLib\ARMExecutor.cs(16,21): Error CS0234: The type or namespace name 'Rest' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

##[error]src\Test\LoadTest\ARMLib\ARMExecutor.cs(170,28): Error CS0246: The type or namespace name 'IAzure' could not be found (are you missing a using directive or an assembly reference?)

##[error]src\Test\LoadTest\ARMLib\ARMExecutor.cs(26,26): Error CS0246: The type or namespace name 'IAzure' could not be found (are you missing a using directive or an assembly reference?)

我已经阅读了文档,但我认为我没有得到它。 https://learn.microsoft.com/en-us/dotnet/api/azure.resourcemanager?view=azure-dotnet

azure azure-devops azure-functions nuget-package
2个回答
0
投票

您可以使用ArmClient 类 (Azure.ResourceManager) - Azure for .NET 开发人员 | Microsoft Learn,它是 Azure.ResourceManager 类中 IAzure 的替代品:-

示例代码这里:-

using System;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Compute;
using Azure.ResourceManager.Resources;

ArmClient client = new ArmClient(new DefaultAzureCredential());
string resourceGroupName = "siliconrg";
SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync();
ResourceGroupCollection resourceGroups = subscription.GetResourceGroups();
ResourceGroupResource resourceGroup = await resourceGroups.GetAsync(resourceGroupName);
await foreach (VirtualMachineResource virtualMachine in resourceGroup.GetVirtualMachines())
{
    //previously we would have to take the resourceGroupName and the vmName from the vm object
    //and pass those into the powerOff method as well as we would need to execute that on a separate compute client
    await virtualMachine.PowerOffAsync(WaitUntil.Completed);
}

输出:-

enter image description here

DevOps YAML 管道:-

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

输出:-

enter image description here


0
投票

有人可以帮忙吗? 我也面临同样的问题。 上述使用 ArmClient 类的解决方案没有任何 Iazure 接口,

© www.soinside.com 2019 - 2024. All rights reserved.