The directory service is unavailable (System.DirectoryServices)

October 17, 2010 — 4 Comments

Just thought I would post about this quickly as its an error message that I ran into recently that took me quite a while to figure out. So to save other .NET developers spending ages trying to work out why it was happening like I did, I’ll explain how you can get rid of this rather annoying problem.

Symptoms:

You try to use the DirectorySearcher to find some objects in AD but get the error “The directory service is unavailable” (error number 2147016689).

 

Cause and Solution

This is actually be caused by the structure of your search filter, and nothing to do with the directory service being unavailable. Whilst some LDAP query utilities will let you use a filter like this to specify an “AND” operation (ie only objects that match all parts of the filter will be returned):

(objectClass=computer)(operatingSystem=Windows*)

It seems that the .NET DirectorySearcher does not like this and you need to explicitly add the ampersand character in there, like so:

(&(objectClass=computer)(operatingSystem=Windows*))

and that simple action should stop you getting this error 🙂

4 responses to The directory service is unavailable (System.DirectoryServices)

  1. 

    Thanks! You just saved me from barking up the wrong tree!

  2. 

    In my case, the username for the account that the context is created with was incorrect.

  3. 

    Thanks for saving me some time. You rock!

  4. 

    In my case the error occurred because the DC was being rebooted just when my script was halfway through exporting all users to a CSV file.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s