public static ReadOnlyCollectionPage<LoadBalancer> ListLoadBalancers(this ILoadBalancerService service, LoadBalancerId markerId, int? limit)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            try
            {
                return service.ListLoadBalancersAsync(markerId, limit, CancellationToken.None).Result;
            }
            catch (AggregateException ex)
            {
                ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
                if (innerExceptions.Count == 1)
                    throw innerExceptions[0];

                throw;
            }
        }
        public static LoadBalancer GetLoadBalancer(this ILoadBalancerService service, LoadBalancerId loadBalancerId)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            try
            {
                return service.GetLoadBalancerAsync(loadBalancerId, CancellationToken.None).Result;
            }
            catch (AggregateException ex)
            {
                ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
                if (innerExceptions.Count == 1)
                    throw innerExceptions[0];

                throw;
            }
        }
        /// <summary>
        /// Gets all existing load balancer nodes through a series of asynchronous operations,
        /// each of which requests a subset of the available nodes.
        /// </summary>
        /// <param name="provider">The load balancer service.</param>
        /// <param name="limit">The maximum number of <see cref="Node"/> to return from a single task. If this value is <c>null</c>, a provider-specific default is used.</param>
        /// <returns>
        /// A collection of <see cref="Node"/> objects, each of which represents a subset
        /// of the available load balancer nodes.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="provider"/> is <c>null</c>.
        /// <para>-or-</para>
        /// <para>If <paramref name="loadBalancerId"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="ArgumentException">If <paramref name="loadBalancerId"/> is empty.</exception>
        /// <exception cref="ArgumentOutOfRangeException">If <paramref name="limit"/> is less than or equal to 0.</exception>
        private static IEnumerable<Node> ListAllLoadBalancerNodes(ILoadBalancerService provider, LoadBalancerId loadBalancerId, int? limit, CancellationToken cancellationToken)
        {
            if (provider == null)
                throw new ArgumentNullException("provider");
            if (limit <= 0)
                throw new ArgumentOutOfRangeException("limit");

            // this API call is not currently paginated
            IEnumerable<Node> loadBalancers = provider.ListNodesAsync(loadBalancerId, cancellationToken).Result;
            return loadBalancers;
        }
        private static IEnumerable<NodeServiceEvent> ListAllNodeServiceEvents(ILoadBalancerService provider, LoadBalancerId loadBalancerId, int? limit, CancellationToken cancellationToken)
        {
            if (limit <= 0)
                throw new ArgumentOutOfRangeException("limit");

            NodeServiceEvent lastServiceEvent = null;

            do
            {
                NodeServiceEventId marker = lastServiceEvent != null ? lastServiceEvent.Id : null;
                IEnumerable<NodeServiceEvent> serviceEvents = provider.ListNodeServiceEventsAsync(loadBalancerId, marker, limit, cancellationToken).Result;
                lastServiceEvent = null;
                foreach (NodeServiceEvent serviceEvent in serviceEvents)
                {
                    yield return serviceEvent;
                    lastServiceEvent = serviceEvent;
                }
            } while (lastServiceEvent != null);
        }
        public static ReadOnlyCollection<LoadBalancerUsage> ListHistoricalUsage(this ILoadBalancerService service, LoadBalancerId loadBalancerId, DateTimeOffset? startTime, DateTimeOffset? endTime)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            try
            {
                return service.ListHistoricalUsageAsync(loadBalancerId, startTime, endTime, CancellationToken.None).Result;
            }
            catch (AggregateException ex)
            {
                ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
                if (innerExceptions.Count == 1)
                    throw innerExceptions[0];

                throw;
            }
        }
        public static void RemoveVirtualAddressRange(this ILoadBalancerService service, LoadBalancerId loadBalancerId, IEnumerable<VirtualAddressId> virtualAddressIds)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            try
            {
                service.RemoveVirtualAddressRangeAsync(loadBalancerId, virtualAddressIds, AsyncCompletionOption.RequestSubmitted, CancellationToken.None, null).Wait();
            }
            catch (AggregateException ex)
            {
                ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
                if (innerExceptions.Count == 1)
                    throw innerExceptions[0];

                throw;
            }
        }
        public static LoadBalancerVirtualAddress AddVirtualAddress(this ILoadBalancerService service, LoadBalancerId loadBalancerId, LoadBalancerVirtualAddressType type, AddressFamily addressFamily)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            try
            {
                return service.AddVirtualAddressAsync(loadBalancerId, type, addressFamily, AsyncCompletionOption.RequestSubmitted, CancellationToken.None, null).Result;
            }
            catch (AggregateException ex)
            {
                ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
                if (innerExceptions.Count == 1)
                    throw innerExceptions[0];

                throw;
            }
        }
        public static void UpdateNode(this ILoadBalancerService service, LoadBalancerId loadBalancerId, NodeId nodeId, NodeUpdate configuration)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            try
            {
                service.UpdateNodeAsync(loadBalancerId, nodeId, configuration, AsyncCompletionOption.RequestSubmitted, CancellationToken.None, null).Wait();
            }
            catch (AggregateException ex)
            {
                ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
                if (innerExceptions.Count == 1)
                    throw innerExceptions[0];

                throw;
            }
        }
        public static ReadOnlyCollection<Node> AddNodeRange(this ILoadBalancerService service, LoadBalancerId loadBalancerId, IEnumerable<NodeConfiguration> nodeConfigurations)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            try
            {
                return service.AddNodeRangeAsync(loadBalancerId, nodeConfigurations, AsyncCompletionOption.RequestSubmitted, CancellationToken.None, null).Result;
            }
            catch (AggregateException ex)
            {
                ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
                if (innerExceptions.Count == 1)
                    throw innerExceptions[0];

                throw;
            }
        }
        public static void SetErrorPage(this ILoadBalancerService service, LoadBalancerId loadBalancerId, string content)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            try
            {
                service.SetErrorPageAsync(loadBalancerId, content, AsyncCompletionOption.RequestSubmitted, CancellationToken.None, null).Wait();
            }
            catch (AggregateException ex)
            {
                ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
                if (innerExceptions.Count == 1)
                    throw innerExceptions[0];

                throw;
            }
        }
        public static void RemoveNodeMetadataItem(this ILoadBalancerService service, LoadBalancerId loadBalancerId, NodeId nodeId, IEnumerable<MetadataId> metadataIds)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            try
            {
                service.RemoveNodeMetadataItemAsync(loadBalancerId, nodeId, metadataIds, CancellationToken.None).Wait();
            }
            catch (AggregateException ex)
            {
                ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
                if (innerExceptions.Count == 1)
                    throw innerExceptions[0];

                throw;
            }
        }
        public static void UpdateLoadBalancerMetadataItem(this ILoadBalancerService service, LoadBalancerId loadBalancerId, MetadataId metadataId, string value)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            try
            {
                service.UpdateLoadBalancerMetadataItemAsync(loadBalancerId, metadataId, value, CancellationToken.None).Wait();
            }
            catch (AggregateException ex)
            {
                ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
                if (innerExceptions.Count == 1)
                    throw innerExceptions[0];

                throw;
            }
        }
        public static ReadOnlyCollection<LoadBalancerMetadataItem> AddNodeMetadata(this ILoadBalancerService service, LoadBalancerId loadBalancerId, NodeId nodeId, IEnumerable<KeyValuePair<string, string>> metadata)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            try
            {
                return service.AddNodeMetadataAsync(loadBalancerId, nodeId, metadata, CancellationToken.None).Result;
            }
            catch (AggregateException ex)
            {
                ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
                if (innerExceptions.Count == 1)
                    throw innerExceptions[0];

                throw;
            }
        }
        public static void RemoveAccessList(this ILoadBalancerService service, LoadBalancerId loadBalancerId, NetworkItemId networkItemId)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            try
            {
                service.RemoveAccessListAsync(loadBalancerId, networkItemId, AsyncCompletionOption.RequestSubmitted, CancellationToken.None, null).Wait();
            }
            catch (AggregateException ex)
            {
                ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
                if (innerExceptions.Count == 1)
                    throw innerExceptions[0];

                throw;
            }
        }