private object HandleResponse(SearchResponse response) { response.AssertSuccess(); return(response.Entries.Count > 0 ? Options.GetTransformer().Transform(response.Entries[0]) : Options.GetTransformer().Default()); }
private object HandleResponse(SearchResponse response) { response.AssertSuccess(); if (response.Entries.Count == 0) { throw new InvalidOperationException(string.Format("First returned {0} results for '{1}' against '{2}'", response.Entries.Count, SearchRequest.Filter, SearchRequest.DistinguishedName)); } return(Options.GetTransformer().Transform(response.Entries[0])); }
private object HandleResponse(SearchResponse response) { response.AssertSuccess(); if (response.Entries.Count > 1) { throw new InvalidOperationException(string.Format("SingleOrDefault returned {0} results for {1}", response.Entries.Count, SearchRequest.Filter)); } return(response.Entries.Count == 1 ? Options.GetTransformer().Transform(response.Entries[0]) : Options.GetTransformer().Default()); }
private object HandleResponse(SearchResponse response) { response.AssertSuccess(); return(response.Entries.Count > 0); }
/// <summary> /// Uses range retrieval to get all values for <paramref name="attributeName"/> on <paramref name="distinguishedName"/>. /// </summary> /// <typeparam name="TValue">The type of the attribute. Must be <see cref="string"/> or <see cref="Array"/> of <see cref="byte"/>.</typeparam> /// <param name="connection">The connection to the directory.</param> /// <param name="log">The log for query information. Defaults to null.</param> /// <param name="distinguishedName">The distinguished name of the entry.</param> /// <param name="attributeName">The attribute to load.</param> /// <param name="start">The starting point for the range. Defaults to 0.</param> /// <param name="resultProcessing">How the async results are processed</param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="distinguishedName"/> or <paramref name="attributeName"/> is null, empty or white space. /// </exception> /// <returns></returns> public static async Task <IList <TValue> > RetrieveRangesAsync <TValue>(this LdapConnection connection, string distinguishedName, string attributeName, int start = 0, ILinqToLdapLogger log = null, PartialResultProcessing resultProcessing = LdapConfiguration.DefaultAsyncResultProcessing) { int idx = start; int step = 0; string currentRange = null; try { if (connection == null) { throw new ArgumentNullException("connection"); } if (distinguishedName.IsNullOrEmpty()) { throw new ArgumentNullException("distinguishedName"); } if (attributeName.IsNullOrEmpty()) { throw new ArgumentNullException("attributeName"); } //Code pulled from http://dunnry.com/blog/2007/08/10/RangeRetrievalUsingSystemDirectoryServicesProtocols.aspx var list = new List <TValue>(); var range = string.Format("{0};range={{0}}-{{1}}", attributeName); currentRange = string.Format(range, idx, "*"); var request = new SearchRequest { DistinguishedName = distinguishedName, Filter = string.Format("({0}=*)", attributeName), Scope = SearchScope.Base }; request.Attributes.Add(currentRange); bool lastSearch = false; while (true) { SearchResponse response = null; #if NET45 await System.Threading.Tasks.Task.Factory.FromAsync( (callback, state) => { return(connection.BeginSendRequest(request, resultProcessing, callback, state)); }, (asyncresult) => { response = connection.EndSendRequest(asyncresult) as SearchResponse; }, null ).ConfigureAwait(false); #else response = await Task.Run(() => connection.SendRequest(request) as SearchResponse).ConfigureAwait(false); #endif response.AssertSuccess(); if (response.Entries.Count != 1) { break; } SearchResultEntry entry = response.Entries[0]; foreach (string attrib in entry.Attributes.AttributeNames) { currentRange = attrib; lastSearch = currentRange.IndexOf("*", 0, StringComparison.Ordinal) > 0; step = entry.Attributes[currentRange].Count; } foreach (TValue member in entry.Attributes[currentRange].GetValues(typeof(TValue))) { list.Add(member); idx++; } if (lastSearch) { break; } currentRange = string.Format(range, idx, (idx + step)); request.Attributes.Clear(); request.Attributes.Add(currentRange); } return(list); } catch (Exception ex) { if (log != null) { log.Error(ex, string.Format("An error occurred while trying to retrieve ranges of type '{0}' for range '{1}'.", typeof(TValue).FullName, currentRange)); } throw; } }
private async System.Threading.Tasks.Task <object> HandleStandardRequestAsync(LdapConnection connection, ILinqToLdapLogger log, int maxSize, bool pagingEnabled) { if (Options.YieldNoResults) { return(ObjectActivator.CreateGenericInstance(typeof(List <>), Options.GetEnumeratorReturnType(), null, null)); } int pageSize = 0; int index = 0; if (pagingEnabled) { int maxPageSize = Options.PageSize ?? maxSize; pageSize = Options.TakeSize.HasValue ? Math.Min(Options.TakeSize.Value, maxPageSize) : maxPageSize; index = SearchRequest.Controls.Add(new PageResultRequestControl(pageSize)); } if (log != null && log.TraceEnabled) { log.Trace(SearchRequest.ToLogString()); } var list = new List <SearchResultEntry>(); SearchResponse response = null; #if NET45 await System.Threading.Tasks.Task.Factory.FromAsync( (callback, state) => { return(connection.BeginSendRequest(SearchRequest, Options.AsyncProcessing, callback, state)); }, (asyncresult) => { response = (SearchResponse)connection.EndSendRequest(asyncresult); response.AssertSuccess(); list.AddRange(response.Entries.GetRange()); }, null ).ConfigureAwait(false); #else response = await System.Threading.Tasks.Task.Run(() => connection.SendRequest(SearchRequest) as SearchResponse).ConfigureAwait(false); response.AssertSuccess(); list.AddRange(response.Entries.GetRange()); #endif if (pagingEnabled) { if (response.Entries.Count > 0) { var pageResultResponseControl = GetControl <PageResultResponseControl>(response.Controls); bool hasMoreResults = pageResultResponseControl != null && pageResultResponseControl.Cookie.Length > 0 && (!Options.TakeSize.HasValue || list.Count < Options.TakeSize.Value); while (hasMoreResults) { SearchRequest.Controls[index] = new PageResultRequestControl(pageSize) { Cookie = pageResultResponseControl.Cookie }; if (log != null && log.TraceEnabled) { log.Trace(SearchRequest.ToLogString()); } #if NET45 await System.Threading.Tasks.Task.Factory.FromAsync( (callback, state) => { return(connection.BeginSendRequest(SearchRequest, Options.AsyncProcessing, callback, state)); }, (asyncresult) => { response = (SearchResponse)connection.EndSendRequest(asyncresult); response.AssertSuccess(); pageResultResponseControl = GetControl <PageResultResponseControl>(response.Controls); hasMoreResults = pageResultResponseControl != null && pageResultResponseControl.Cookie.Length > 0 && (!Options.TakeSize.HasValue || list.Count <= Options.TakeSize.Value); list.AddRange(response.Entries.GetRange()); }, null ).ConfigureAwait(false); #else response = await System.Threading.Tasks.Task.Run(() => connection.SendRequest(SearchRequest) as SearchResponse).ConfigureAwait(false); response.AssertSuccess(); pageResultResponseControl = GetControl <PageResultResponseControl>(response.Controls); hasMoreResults = pageResultResponseControl != null && pageResultResponseControl.Cookie.Length > 0 && (!Options.TakeSize.HasValue || list.Count <= Options.TakeSize.Value); list.AddRange(response.Entries.GetRange()); #endif } } } AssertSortSuccess(response.Controls); if (Options.TakeSize.HasValue && list.Count > Options.TakeSize.Value) { var size = Options.TakeSize.Value; list.RemoveRange(size, list.Count - size); } var enumerator = Options.GetEnumerator(list); return(ObjectActivator.CreateGenericInstance(typeof(List <>), Options.GetEnumeratorReturnType(), new[] { enumerator }, null)); }