c# – 如何在使用DirectorySearcher时确定ClientTimeout
作者:互联网
使用System.DirecoryServices.DirectorySearcher时,如何确定ClientTimeOut是否已发生或搜索是否自然返回空的SearchResultCollection?
给出以下片段
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
}
}
}
解决方法:
设置ClientTimeout仅在将Asynchronous属性设置为true时才有用.你没做的.非托管IDirectorySearcher接口的文档更加详细.从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.
请注意,当您使用托管类时,这种“某种情况控制”并不完美. SearchResultCollection包装类实际上并没有为您提供一种干净的异步搜索方式,它没有“BeginMoveNext”方法来迭代下一个结果. “继续其他任务”的角度是理论上的.最好不要使用该属性.
标签:c,net,active-directory,directoryservices,adsi 来源: https://codeday.me/bug/20190517/1120581.html