"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()
at System.DirectoryServices.AccountManagement.ADDNLinkedAttrSet.BookmarkAndReset()
at System.DirectoryServices.AccountManagement.PrincipalCollection.get_Count()
GroupPrincipal Group = GroupPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain, "demonstration.int"),"SampleGroup");
MessageBox.Show(Group.Members.Count.ToString());
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