"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

System.DirectoryServices.AccountManagement.

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");
MessageBox.Show(Group.Members.Count.ToString());
 
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.

Comments

  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

    ReplyDelete

Post a Comment

Popular posts from this blog

Windows Server 2016, 2019, 2022, Windows 10 and Windows 11: Date and time "Some settings are managed by your organization".

TFTPD32 or TFTPD64 reports Bind error 10013 An attempt was made to access a socket in a way forbidden by its access permissions.

Windows Server 2019 desktop icons such as My Computer, Windows cannot access the specified device, path, or file.