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.


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):


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


and that simple action should stop you getting this error 🙂

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


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


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


    Thanks for saving me some time. You rock!


    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 to mosaic Cancel reply

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

You are commenting using your 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 )

Connecting to %s