.comment-link {margin-left:.6em;}
Books & Articles I wrote.

Wednesday, February 08, 2006

 

Please dispose your directory connections

I have seen many people open a directory connection but not dispose it afterwards.

Please make sure you dispose of your directory connections - the easiest way is to use the using keywork as follows:

using (DirectoryEntry groupEntry = new DirectoryEntry("LDAP://"
+ LdapMachineName
+ ":"
+ LdapPort
+ LdapPartition
+ LdapReadersGroup,
LdapUserName,
LdapUserPassword,
AuthenticationTypes.Secure))
{
...
}

If you do not dispose them then the underlying COM connection remains open (ADSI takes care of the connection to the directory by itself, so don't worry about that) and you end up with a memory leak because the finalizer of the DirectoryEntry never calls unbind.

This page is powered by Blogger. Isn't yours?

Weblog Commenting and Trackback by HaloScan.com