DirectorySearcher FindAll SearchResultCollection计数引发COMException [英] DirectorySearcher FindAll SearchResultCollection Count throws COMException

查看:179
本文介绍了DirectorySearcher FindAll SearchResultCollection计数引发COMException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了让某人登录我的应用程序,我已经使用了很长时间的代码了:

 私有员工身份验证(字符串userName,字符串密码){
DirectorySearcher search = new DirectorySearcher(_rootDirectory);
search.Filter =(&(objectClass = user)(SAMAccountName = + userName +)) ;;
试试{
SearchResultCollection结果= search.FindAll();
if(0< results.Count){
//我其余的代码
//返回雇员
//如果密码与
匹配
} catch(Exception err){
MessageBox.Show(err.Message, ActiveDir.cs ADWrapper :: AuthenticateUser Error,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
返回null;
}

最近,代码一直在抛出方法正在线程中执行,并且我正在线程完成之前检查结果。



有没有办法告诉 FindAll()是何时完成,还是我刚刚发现了由于 Active Directory 更新?

解决方案

这是调试器工件。调试表达式在进程内部的帮助程序线程上执行。它是与执行代码的线程不同的 线程这一事实可能会产生副作用。例如,很明显的情况是在其getter中使用 lock 的属性。



不太明显的是与COM相关的任何事物,例如活动目录。 COM为不是线程安全的COM服务器实现线程安全。这在调试器线程上无法正常运行,创建服务器的线程已冻结。再加上其他可能出错的内容,例如不需要封送呼叫等的代理/存根。



您没有真正的问题。


I've got some code that has been working fine for a long time to get someone logged in on my application:

private Employee Authenticate(string userName, string password) {
  DirectorySearcher search = new DirectorySearcher(_rootDirectory);
  search.Filter = "(&(objectClass=user)(SAMAccountName=" + userName + "))";
  try {
    SearchResultCollection results = search.FindAll();
    if (0 < results.Count) {
      // the rest of my code
      // that returns an employee
      // if the password matches
    }
  } catch (Exception err) {
    MessageBox.Show(err.Message, "ActiveDir.cs ADWrapper::AuthenticateUser Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  }
  return null;
}

Lately, the code has been throwing a COMException as soon as I test the results.Count value (the SearchResultCollection is not null).

Microsoft's Documentation does not indicate that Count should be throwing any kind of exceptions.

When debugging my code, I can put the break point on the condition above, mouse over it, and see that the exception exists.

If I use F10 to let the debugger take me to the catch condition or wait a few seconds on the breakpoint, the results.Count variable becomes valid and contains an integer value.

I am guessing the FindAll method is executing in a thread, and that I am checking the results before the thread has completed.

Is there a way to tell when FindAll() has finished or have I just spotted some kind of new bug that happened as a result of an Active Directory update?

解决方案

This is a debugger artifact. Debug expressions are executed on a helper thread inside the process. The fact that it is a different thread from the one that the code is executing on can have side-effects. Obvious cases are, say, properties that use lock in their getter.

The not-so-obvious ones are anything that's COM related, like Active Directory. COM implements thread-safety for COM servers that are not thread-safe. This can't work well on the debugger thread, the thread that created the server is frozen. Plus anything else that can go wrong, like not having a proxy/stub required to marshal the call etcetera.

You don't have a real problem.

这篇关于DirectorySearcher FindAll SearchResultCollection计数引发COMException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆