locked
Carrying objects between searches: Efficent way of using DirectorySearcher RRS feed

  • Question

  • User1588321482 posted

    I am new to VB.Net and in the past have used VBS. There I would use ADO to search directories and try to pass the ADODB object between searches rather than destroying and re-intiating between calls. The Directory Searcher class looks much the same but I want to make sure I am using if efficently.

     As an example if I want to search my Exchange 5.5 Directory then a couple of Active Directory Forests (we have 4 Forests, plus two iPlanet Directories)

    1    'Create .Net Directory Search Object
    2    Dim DirEntry As New DirectoryServices.DirectoryEntry(LDAPPath)
    3    Dim SearchDir As New DirectoryServices.DirectorySearcher(DirEntry)
    4    SearchDir.Filter = "(&(uid=" & alias to search for & "))"
    5   
    6    'one dimensional string array to set the attribute list of the search
    7    SearchDir.PropertiesToLoad.AddRange(colAttributes)
    8   
    9    'Create Search Collection and Result to hold values from query
    10   Dim searchReturn As DirectoryServices.SearchResultCollection
    11   Dim SearchResult As DirectoryServices.SearchResult

     It looks to me that I can't carry anything between searches becase the LDAPPath for the DirectoryServices.DirectoryEntry is different and this is the route object. However, can I refresh the object rather than destryoing and recreating? What about the other parameters? The Search Filter will be different (Exchange 5.5 I am using UID (which is alias) whilst in Active Directory it becomes mailnickname). The collection of attributes that I want to return will also be different because of the different naming conventions between directories.

    Finally, how do I clear up properly at the end of the procedure? Again from my VBS experience I would use a combination of 'object.close' and 'Set object = Nothing'?

    Many thanks,

     Paul 

    Sunday, June 24, 2007 8:23 AM

Answers

  • User1439985827 posted

    Each DirectoryEntry wraps one AdsObject, so I don't think there is a way to preserve it or reuse it.

    The proper way to do cleanup is to call Dispose() on your Searcher and DirectoryEntry when you are done using them. You don't have to set them to Nothing. However usually you will notice problems with the DirectoryEntry if you don't call dispose. The most common exceptions are CannotUnloadAppDomain and random ComExceptions.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 24, 2007 10:58 AM

All replies

  • User1439985827 posted

    Each DirectoryEntry wraps one AdsObject, so I don't think there is a way to preserve it or reuse it.

    The proper way to do cleanup is to call Dispose() on your Searcher and DirectoryEntry when you are done using them. You don't have to set them to Nothing. However usually you will notice problems with the DirectoryEntry if you don't call dispose. The most common exceptions are CannotUnloadAppDomain and random ComExceptions.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 24, 2007 10:58 AM
  • User1588321482 posted

    Chris,

     Thanks for that it has confirmed what I was assuming. The clarification on Dispose is handy as well.

    I found this useful article on the Technet site that is now in my favorites (sic) as it gives good advice on building efficient filters for LDAP:

     http://msdn.microsoft.com/en-us/library/ms808539.aspx

     

    Sunday, June 24, 2007 11:15 AM