public void TestAsyncSearch() { using (LdapConnection connection = GetConnection()) { string ouName = "ProtocolsGroup9"; string dn = "ou=" + ouName; try { for (int i = 0; i < 20; i++) { DeleteEntry(connection, "ou=ProtocolsSubGroup9." + i + "," + dn); } DeleteEntry(connection, dn); AddOrganizationalUnit(connection, dn); SearchResultEntry sre = SearchOrganizationalUnit(connection, LdapConfiguration.Configuration.SearchDn, ouName); Assert.NotNull(sre); for (int i = 0; i < 20; i++) { AddOrganizationalUnit(connection, "ou=ProtocolsSubGroup9." + i + "," + dn); } string filter = "(objectClass=organizationalUnit)"; SearchRequest searchRequest = new SearchRequest( dn + "," + LdapConfiguration.Configuration.SearchDn, filter, SearchScope.OneLevel, null); ASyncOperationState state = new ASyncOperationState(connection); IAsyncResult asyncResult = connection.BeginSendRequest( searchRequest, PartialResultProcessing.ReturnPartialResultsAndNotifyCallback, RunAsyncSearch, state); asyncResult.AsyncWaitHandle.WaitOne(); Assert.True(state.Exception == null, state.Exception == null ? "" : state.Exception.ToString()); } finally { for (int i = 0; i < 20; i++) { DeleteEntry(connection, "ou=ProtocolsSubGroup9." + i + "," + dn); } DeleteEntry(connection, dn); } } }
private static void RunAsyncSearch(IAsyncResult asyncResult) { ASyncOperationState state = (ASyncOperationState)asyncResult.AsyncState; try { if (!asyncResult.IsCompleted) { PartialResultsCollection partialResult = null; partialResult = state.Connection.GetPartialResults(asyncResult); if (partialResult != null) { for (int i = 0; i < partialResult.Count; i++) { if (partialResult[i] is SearchResultEntry) { Assert.Contains("Group9", ((SearchResultEntry)partialResult[i]).DistinguishedName); } } } } else { SearchResponse response = (SearchResponse)state.Connection.EndSendRequest(asyncResult); if (response != null) { foreach (SearchResultEntry entry in response.Entries) { Assert.Contains("Group9", entry.DistinguishedName); } } } } catch (Exception e) { state.Exception = e; } }