簡體   English   中英

如何使用 Azure Mgmt SDK fluent 獲取所有快照

[英]How to get all snapshot by using of Azure Mgmt SDK fluent

我正在使用Fluent以編程方式獲取 Azure 中的資源(C# .NET-Core Web 應用程序)並嘗試通過提供如下服務主體來獲取資源信息:

string subscriptionId="XXX"; 
AzureCredentials cred = new AzureCredentialsFactory()
                      .FromServicePrincipal(UIConstants.ClientID, 
                       UIConstants.Secret, UIConstants.Tenant,AzureEnvironment
                      .AzureGlobalCloud);                      
            
var azure = Azure.Configure()
            .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) 
            .Authenticate(cred) 
            .WithSubscription(subscriptionId);

當我試圖像這樣獲取所有快照時

foreach (var s in azure.Snapshots.List())
{
//get snapshot
}

但是沒有錯誤,循環也沒有執行。 C# 中是否有任何代碼示例可以獲取所有快照信息。

正如評論中提到的,這是因為那里沒有快照。

實際上,在循環之前,您可以確定是否返回了快照。 如下代碼:

using System.Linq;

//your other code

 //convert to list
 var mysanpshots = azure.Snapshots.List().ToList();            

 //if there are snapshots existing
 if (mysanpshots.Count > 0)
 {
      foreach (var s in mysanpshots)
      {
          //code
      }
 }
 //if no snapshots
 else
 {
    //code
 }

暫無
暫無

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

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