gpt4 book ai didi

c# - 使用 DirectorySearcher 时如何确定 ClientTimeout

转载 作者:太空狗 更新时间:2023-10-29 23:07:52 25 4
gpt4 key购买 nike

当使用 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
}
}
}

最佳答案

只有当您将 Asynchronous 属性设置为 true 时,设置 ClientTimeout 才有用。你没有做。非托管 IDirectorySearcher 接口(interface)的文档更加详细。来自 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# - 使用 DirectorySearcher 时如何确定 ClientTimeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21188618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com