Tuesday, 11 December 2012

"Object reference not set to an instance of an object" when reading C# GroupPrincipal.Members.Count

It seems that there is a bug in the .NET 3.5 managed library


When you try and view the Members.Count property of a Global group the system throws the following exception.

System.NullReferenceException: Object reference not set to an instance of an object.
at System.DirectoryServices.AccountManagement.ADDNLinkedAttrSet.BookmarkAndReset()
at System.DirectoryServices.AccountManagement.PrincipalCollection.get_Count()
It is however possible to enumerate the members of the group
The following sample code demonstrates the issue where the domain is called demonstration.int and the group name is SampleGroup.
GroupPrincipal Group = GroupPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain, "demonstration.int"),"SampleGroup");
If anyone has any further information please feel free to post a comment. I've not seen any other information on the issue on the net.

1 comment:

  1. I faced the same problem and made a work around for it by using [GetMembers(true)] method of [GroupPrincipal] and get the count from the returned collection