Sometimes I get the following exception: a COM object that has been separated from its base RCW cannot be used
Code example:
using (AdOrganizationalUnit organizationalUnit = new AdOrganizationalUnit(ADHelper.GetDirectoryEntry(ouAdDn)))
{
using (AdUser user = organizationalUnit.AddUser(commonName))
{
user.Properties[key].Add(value);
user.CommitChanges();
user.SetPassword(password);
user.Properties["UserAccountControl"].Value = 512;
user.CommitChanges();
}
}
AdUser is as follows:
public class AdUser : DirectoryEntry
{
public AdUser(DirectoryEntry entry)
: base(entry.NativeObject)
{
}
public bool SetPassword(string password)
{
object result = this.Invoke("SetPassword", new object[] { password });
return true;
}
}
This is a simplified version of my code. Sometimes an exception occurs, sometimes not. In most cases, this happens when I try to set the UserAccountControl value. Does anyone know what the reason is?
I found out that this error occurs when I install the DirectoryEntry with which AdUser was created, and I'm still trying to use the AdUser object. However, this is not the case in the code above. Is it possible that DirectoryEntry somehow disposes?
, . , SecurityDescriptor , 200-300 . , . raceonrcwcleanup. .
.