0

使用Microsoft.Azure.Management.ResourceManager.Fluent库枚举资源组中的容器时:

azure.ContainerGroups.ListByResourceGroup(resouceGroup)

返回一个枚举器,但对枚举器执行任何操作(例如.ToList())都会抛出:

Exception thrown: 'Microsoft.Rest.Azure.CloudException' in System.Private.CoreLib.dll The Resource 'Microsoft.ContainerInstance/containerGroups/myResource' under resource group 'myResourceGroup' was not found.

4

1 回答 1

2

我通过重建枚举器来解决它,因为它在MoveNext()操作中抛出。然而,枚举器迭代器仍然增加,有效地跳过了丢失的资源。

    private static List<IContainerGroup> getContainerInstances(IAzure azure, string resouceGroup)
    {
        var brokenEnumerator = azure.ContainerGroups.ListByResourceGroup(resouceGroup).GetEnumerator();
        var containerInstances = new List<IContainerGroup>();

        while (true)
        {
            try
            {
                if (!brokenEnumerator.MoveNext())
                {
                    break;
                }
                containerInstances.Add(brokenEnumerator.Current);
            }
            catch (CloudException)
            {
                // noop
            }
        }

        return containerInstances;
    }
于 2018-08-09T08:29:21.750 回答