Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine ClientTimeout when using DirectorySearcher

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                            
                            }
                        }
                    }
like image 242
flalar Avatar asked Jan 17 '14 14:01

flalar


1 Answers

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.

like image 121
Hans Passant Avatar answered Nov 15 '22 04:11

Hans Passant