azure.ContainerGroups.ListByResourceGroup引发资源未找到异常

时间:2018-08-09 08:25:47

标签: azure-resource-manager azure-container-instances

使用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.

1 个答案:

答案 0 :(得分:1)

我通过重建枚举数解决了它,因为它引发了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;
    }