簡體   English   中英

.NET Core 2.0中的擴展方法用法?

[英]Extension Method usage in .NET Core 2.0?

我正在嘗試使用Microsoft.Azure.Management.Compute ComputeManagementClient Usage屬性來列出通過Azure的所有資源使用情況。

我專門使用Usage.ListAsync()實現此目的。 根據文檔,這是一種擴展方法。

我使用以下方法創建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並未指出名稱空間的使用情況(灰色,然后是黑色)。

您錯誤地調用了Usage.ListAsync()方法-看起來需要一個字符串參數:

ListAsync(IUsageOperations, String, CancellationToken)

獲取指定位置的當前計算資源使用情況信息以及訂閱下的計算資源限制。

實際上,在代碼中,它提到您需要一個位置:

在此處輸入圖片說明

因此,將您的代碼更新為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM