我使用的是.net 4.7.2 (非核心)和C#。
我需要想出一种方法来不阻止我当前的异步任务,我需要搜索用户作为这些任务的一部分。我之前做过DirectorySearcher操作,所以我知道附加到AD和第一次搜索可能需要几秒钟的时间,如果我尝试从现有的异步方法调用它,这将真的是一件很糟糕的事情。
我发现DirectorySearcher有一个“异步”属性。但我认为它不支持异步模式。
DirectorySearcher ds = new DirectorySearcher();
ds.Asynchronous = true;
ds.Filter = "(&(objectclass=user)(samaccountname=testaccount)";
ds.PropertiesToLoad.Add("samaccountname");
SearchResult sr = await ds.FindOne();当然,最后一行会抛出错误,因为FindOne不是异步方法。我已经知道,如果我删除await,它将会编译。但这并不能解决我从现有的等待方法中调用它的问题。我需要找到一种在AD中进行异步搜索的方法...
有人知道如何在.net框架(而不是核心)中使用它吗?
发布于 2020-01-17 06:18:16
没有一款MS产品能做到这一点。
我确实发现了一个名为ldap4net的活跃的nuget项目,它可以做到这一点。
发布于 2020-01-16 07:43:28
尝试在线程池中运行它。
private async void MyAsyncMethod()
{
// do some asynchronous thing
// await something
// then, run below on thread pool, which would not block MyAsyncMethod
Task.Run(() =>
{
DirectorySearcher ds = new DirectorySearcher();
ds.Asynchronous = true;
ds.Filter = "(&(objectclass=user)(samaccountname=testaccount)";
ds.PropertiesToLoad.Add("samaccountnamt");
SearchResult sr = ds.FindOne();
});
}参考:https://docs.microsoft.com/dotnet/api/system.threading.tasks.task.run
https://stackoverflow.com/questions/59760951
复制相似问题