【问题标题】:Extension Method usage in .NET Core 2.0?.NET Core 2.0 中的扩展方法用法?
【发布时间】:2018-01-30 04:34:55
【问题描述】:

我正在尝试使用 Microsoft.Azure.Management.Compute ComputeManagementClient Usage 属性来列出通过 Azure 的所有资源使用情况。

我专门使用Usage.ListAsync() 来实现这一点。根据documentation,这是一种扩展方法。

我在使用以下创建 ComputeManagementClient 的实例后调用扩展方法:

    public async Task<IComputeUsage> GetResourceUsage(int customerId)
    {
        var azureClient = _dbContext.CreateAzureComputeClient(customerId);

        var usageReport = await azureClient.Usage.ListAsync();
    }

但是,这将返回以下错误:

'IUsageOperations' does not contain a definition for Listasync and the best extention method overload ProvidersOptionsExtensions.ListAsync... Requires a receiver of type 'IProvidersOperations'

这是一个错误,还是我错误地调用了该方法?

编辑:我在代码中使用了正确的命名空间:

using System.Threading.Tasks;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.Compute.Fluent;

然而,VS2017 并没有指明命名空间的使用(灰色,而不是黑色)。

【问题讨论】:

  • 要使用扩展方法,您需要命名空间:using Microsoft.Azure.Management.Compute。除此之外,他们的文档中没有无参数的ListAsync
  • @CodingYoshi 虽然这是我的第一个,但我在我的代码中使用了这个命名空间。

标签: c# .net azure asp.net-core-2.0


【解决方案1】:

您错误地调用了Usage.ListAsync() 方法 - 看起来需要一个字符串参数:

ListAsync(IUsageOperations, String, CancellationToken)

获取,对于 指定位置,当前计算资源使用信息为 以及订阅下的计算资源限制。

确实,在代码中,它提到您需要一个位置:

所以将您的代码更新为:

var usageReport = await azureClient.Usage.ListAsync("[resourceId('Microsoft.Storage/storageAccounts','examplestorage')]");

【讨论】:

  • 是的,很抱歉我正在更新自己的答案。似乎方法扩展错误是一个红鲱鱼,它确实需要一个位置!安装命名空间后输入位置解决了问题。
猜你喜欢
  • 2018-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 2020-01-30
  • 2018-12-14
  • 1970-01-01
相关资源
最近更新 更多