1

I'm working with System.DirectoryServices and I have the following method that I use to create the DirectoryEntry:

static DirectoryEntry CreateDirectoryEntry(string connectionPath) { DirectoryEntry ldapConnection = null; try { ldapConnection = new DirectoryEntry(AD_DOMAIN_NAME)) ldapConnection.Path = connectionPath; ldapConnection.AuthenticationType = AuthenticationTypes.Secure; } catch (Exception ex) { MessageBox.Show("Exception Caught in createDirectoryEntry():\n\n" + ex.ToString()); } return ldapConnection; } 

This method is called in this manner:

DirectoryEntry ldapConnection = CreateDirectoryEntry("LDAP://OU=Example,DC=domain,DC=com"); 

I read that it is best practice to use a using statement for anything that implements IDisposable. My question is, do I need a using statement just in the CreateDirectoryEntry() method or should I also do it for each call?

To illustrate what I mean, is this sufficient?:

static DirectoryEntry CreateDirectoryEntry(string connectionPath) { DirectoryEntry ldapConnection = null; try { using (ldapConnection = new DirectoryEntry(AD_DOMAIN_NAME)) { ldapConnection.Path = connectionPath; ldapConnection.AuthenticationType = AuthenticationTypes.Secure; } } catch (Exception ex) { MessageBox.Show("Exception Caught in createDirectoryEntry():\n\n" + ex.ToString()); } return ldapConnection; } 

Or would I also need to use a using statement on the call like this?:

using (DirectoryEntry ldapConnection = CreateDirectoryEntry("LDAP://OU=Example,DC=domain,DC=com")) { //Do something with ldapConnection } 

Any help is greatly appreciated!

Note: I can't use System.DirectoryServices.AccountManagement for this solution so please keep answers related to System.DirectoryServices. Thanks!

2
  • 2
    If you need to return it then of course you will have to keep it outside using Commented May 14, 2015 at 17:03
  • Here's the pattern for dealing with a disposable item that you return from a method. The idea is to only dispose of it if and exception occurs and cannot be handled. Commented May 14, 2015 at 17:13

2 Answers 2

2

Your last code-snippet is the way to go, otherwise you'd be returning a disposed ldapConnection to your caller - not a good thing.

And +1 for using using!!

Sign up to request clarification or add additional context in comments.

1 Comment

When you put it like that, it makes so much more sense. I was definitely over-thinking this. Thanks!
0

Your first snippet will dispose ldapConnection before returning it, therefore returning a disposed object which you obviously don't need.

Your function should look something like this:

static DirectoryEntry CreateDirectoryEntry(string connectionPath) { DirectoryEntry ldapConnection = null; try { ldapConnection = new DirectoryEntry(AD_DOMAIN_NAME) ldapConnection.Path = connectionPath; ldapConnection.AuthenticationType = AuthenticationTypes.Secure; } catch (Exception ex) { MessageBox.Show("Exception Caught in createDirectoryEntry():\n\n" + ex.ToString()); } return ldapConnection; } 

While the second snippet is correct: to properly dispose of the DirectoryEntry, you should be using using.

6 Comments

This should include finally to properly dispose of the object if something goes wrong.
@neverendingqs That would be true if the exception wasn't swallowed.
@juharr - finally will always be called even if it goes into the catch.
@neverendingqs No, I mean the finally isn't needed because the exception is caught and nothing is thrown, so the DirectoryEntry will be returned and presumably should not be disposed. Now, should they be swallowing all exceptions here? Probably not.
@Sylverac You might find using Code Analysis useful. It will point out a number of things like that in your code.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.