views:

16

answers:

1

I know random errors don't really exist in our business, but I've got no clue as to why this method is not working.

In short, this method searches for some properties starting from the root domain. It works great, except that it randomly gives me the following exception:

Error

System.ApplicationException: No free/busy public folder found. Possible problem is wrong public folder URI or user doesn't have permission to read free/busy info.

Code

try
{
    using (HostingEnvironment.Impersonate())
    {
        var entry = new DirectoryEntry("LDAP://rootDomain.com");
        var searcher = new DirectorySearcher(string.Format("(&(sAMAccountName={0})(objectCategory=person))",searchAccount));
        var primarysmtp = string.Empty;
        var servername = string.Empty;
        searcher.SearchRoot = entry;
        searcher.SearchScope = SearchScope.Subtree;
        searcher.PropertiesToLoad.Add("msExchHomeServerName");
        searcher.PropertiesToLoad.Add("proxyAddresses");

        //Exception happens here
        results = searcher.FindAll();
    }
}
finally
{
    //Dispose entry, searcher & results
}

I've put some extra logging in this method, which tells me the following:

  • It works for 20 times or more in a row
  • At a random(?) moment, it fails 2-4 times in a row
  • A minute later, everything works again.

I thought about authentication, but I don't see why that would make it fail so 'randomly'. Has anybody seen this kind of behavior before, or can someone give me some clues as to how to fix this?

A: 

Not sure if this will help, but in any case you should be disposing all IDisposable objects. In the sample above:

  • entry (DirectoryEntry)
  • searcher (DirectorySearcher)
  • results (SearchResultCollection)

SearchResultCollection is one that is often forgotten, and doing so can result in a memory leak according to the MSDN documentation.

Joe
Actually, they are disposed of in the finally block of the method. I left it out for brevity, but I will add it to the post.
KoMet