5

I have seen some code for working with AD in this stackoverflow question

I am getting confused about the using statement. I thought that it was just used for things that you are worried could become memory leak, like a WebClient, or similar...

Anyway:

using (var context = new PrincipalContext( ContextType.Domain )) 
 {
      using (var user = UserPrincipal.FindByIdentity( context, "username" ))
      {
          var groups = user.GetAuthorizationGroups();
          ...
      }
 }

when I reach the line var groups = user.GetAuthorizationGroups() - user is null, so that line fails with a NullReference. When I mouse over it debugging it shows null, then shows Static Members, and has all the values.

If I take the line out of using statement and just have var user = UserPrincipal.FindByIdentity( context, "username" ) user is populated as required.

So what's going on ???

Edit: I stuffed up and was sending a bogus username. Oddly though when I check the variables during debug, when you would expect user to be completely null if I sent the bogus userid, but it showed under user: null, static members, and there had values for what I was currently logged in as-so I thought it was to do with the using statement potentially. Cheers!

Community
  • 1
  • 1
baron
  • 11,011
  • 20
  • 54
  • 88
  • `using` is for anything that implements `IDisposable`. – John Saunders Aug 05 '11 at 04:21
  • In more general terms, you should think about the `using` statement (and thus `IDisposable`) being used to prevent *resource leaks*. Such resources ultimately are "memory" but need not be in the more abstract sense (like a file handle, a database connection, etc.). – Christian.K Aug 05 '11 at 04:23
  • 1
    "Using" statements only affect the variable at the end of the Using scope, at which point it disposes of the variable. Are you sure it's not an invalid user being passed in? (as UserPrincipal.FindByIdentity returns null if not found) – Will Aug 05 '11 at 04:23
  • If the code is exactly as you describe, then it should work the same whether the using statement is there or not (except that it may leak resources if the using statement is omitted). There is something else going on you aren't telling us! – Nikki Locke Aug 05 '11 at 04:23

3 Answers3

3

What you're describing cannot happen. The specialness of a using statement doesn't happen until the block is completed, when the object gets disposed. So inside that block, the user variable is the same regardless of whether it's in a using statement or not.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
2

using is just syntactic sugar for try/finally and automatically calls Dispose on objects that implement IDisposable.

Jon Crowell
  • 21,695
  • 14
  • 89
  • 110
Icarus
  • 63,293
  • 14
  • 100
  • 115
1

The C# "using" statement provides a convenient syntax that ensures the correct use of IDisposable objects. Check to see if the PrincipalContext has a disposable method or implements IDisposable. If not, then you should not use "using". See MSDN documentation for more details.

Tarzan
  • 4,270
  • 8
  • 50
  • 70