DirectorySearcher FindAll SearchResultCollection Count throws COMException

≡放荡痞女 提交于 2019-12-13 05:53:53

问题


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?


回答1:


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.




回答2:


I found the exact behaviour as the OP. My problem was that my filter had an error - when this happens I get no error thrown by FindAll() but then trying to access .Count gives the error in the debugger...a few seconds later .Count returns zero.



来源:https://stackoverflow.com/questions/10358269/directorysearcher-findall-searchresultcollection-count-throws-comexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!