Disco.Services.Interop.ActiveDirectory.ADDomain.SearchInternal C# (CSharp) Method

SearchInternal() private method

private SearchInternal ( string SearchRoot, string LdapFilter, string LoadProperties, int ResultLimit ) : IEnumerable
SearchRoot string
LdapFilter string
LoadProperties string
ResultLimit int
return IEnumerable
        internal IEnumerable<ADSearchResult> SearchInternal(string SearchRoot, string LdapFilter, string[] LoadProperties, int? ResultLimit)
        {
            if (string.IsNullOrEmpty(SearchRoot))
                throw new ArgumentNullException("SearchRoot");
            if (string.IsNullOrEmpty(LdapFilter))
                throw new ArgumentNullException("LdapFilter");
            if (ResultLimit.HasValue && ResultLimit.Value < 1)
                throw new ArgumentOutOfRangeException("ResultLimit", "The ResultLimit must be 1 or greater");

            // Search with recovery
            var exceptionCount = 0;
            Queue<Exception> exceptions = null;
            do
            {
                var domainController = GetAvailableDomainController();

                try
                {
                    return domainController.SearchInternal(SearchRoot, LdapFilter, LoadProperties, ResultLimit);
                }
                catch (Exception ex)
                {
                    if (exceptions == null)
                        exceptions = new Queue<Exception>(SearchExceptionRetryMax);

                    exceptions.Enqueue(ex);
                    exceptionCount++;

                    // Set offline for DomainControllerUnavailableMinutes
                    domainController.IsAvailable = false;
                    SystemLog.LogWarning(string.Format("A domain controller [{0}] is offline. It will be retried after {1}. Error: {2} [{3}]", domainController.Name, domainController.AvailableWhen.Value.ToShortTimeString(), ex.Message, ex.GetType().Name));
                }
            } while (exceptionCount < SearchExceptionRetryMax);

            throw new AggregateException(
                new Exception[] { new Exception(string.Format("Unable to perform Active Directory Search after {0} attempts", exceptionCount)) }
                .Concat(exceptions));
        }