When using the System.DirecoryServices.DirectorySearcher how can you determine if a ClientTimeOut has occurred or that the search naturally returned an empty SearchResultCollection?
given the following snippet
using (var searcher = new DirectorySearcher(adRoot))
{
searcher.Filter = "SomeFilter";
searcher.PropertiesToLoad.Add("givenname");
searcher.PropertiesToLoad.Add("sn");
searcher.PropertiesToLoad.Add("department");
searcher.PropertiesToLoad.Add("samaccountname");
searcher.ClientTimeout = TimeSpan.FromSeconds(10);
using (var results = searcher.FindAll())
{
//haldle results
}
}
}
Setting the ClientTimeout is only useful when you set the Asynchronous property to true. Which you didn't do. The documentation for the unmanaged IDirectorySearcher interface is much more detailed. From the MSDN article:
The client time limit preference is useful when a client requests an asynchronous search. In an asynchronous search, the client makes a request and then proceeds with other tasks while waiting for the server to return the results. It is possible that the server can go offline without notifying the client. In this case, the client will have no notification of whether the server is still processing the query, or if it no longer live. The client time limit preference gives the client some control of situations like this.
Do note that this "some control of situations" is less than perfect when you use the managed classes. The SearchResultCollection wrapper class doesn't actually give you a clean way to search asynchronously, it doesn't have a "BeginMoveNext" method to iterate the next result. The "proceeds with other tasks" angle is rather theoretical. Best not to use the property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With