【问题标题】:How to get all snapshot by using of Azure Mgmt SDK fluent如何使用 Azure Mgmt SDK fluent 获取所有快照
【发布时间】:2020-07-14 09:43:34
【问题描述】:

我正在使用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# 中是否有任何代码示例可以获取所有快照信息。

【问题讨论】:

  • 您 100% 确定快照存在?
  • 感谢@mjwills 的回复。我在 azure 门户查看了快照,但没有快照。所以上面的代码是正确的但不存在任何快照'for'循环没有执行..这是正确的假设吗?

标签: c# azure .net-core azure-management-api


【解决方案1】:

正如评论中提到的,这是因为没有快照。

实际上,在循环之前,您可以确定是否有快照返回。如下代码:

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
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 2021-06-07
    相关资源
    最近更新 更多