已经与其底层 RCW 分离的 COM 对象无法使用 - 为什

COM object that has been separated from its underlying RCW can not be used - why does it happen?(已经与其底层 RCW 分离的 COM 对象无法使用 - 为什么会发生?)

本文介绍了已经与其底层 RCW 分离的 COM 对象无法使用 - 为什么会发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时会遇到以下异常:已经与其底层 RCW 分离的 COM 对象无法使用

I sometimes get the following exception: COM object that has been separated from its underlying RCW can not be used

示例代码:

using (AdOrganizationalUnit organizationalUnit = new AdOrganizationalUnit(ADHelper.GetDirectoryEntry(ouAdDn))) 
{ 
using (AdUser user = organizationalUnit.AddUser(commonName)) 
{ 
//set some properties 
user.Properties[key].Add(value); 

user.CommitChanges(); 

user.SetPassword(password); //it is set using Invoke 

//must be set after creating user 
user.Properties["UserAccountControl"].Value = 512; 

user.CommitChanges(); 

} 
} 

AdUser 看起来像这样:

AdUser looks like this:

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; 
} 
} 

这是我的代码的简化版本.异常有时会出现,有时不会.大多数情况下,当我尝试设置 UserAccountControl 值时会发生这种情况.有谁知道可能是什么原因?

This is simplified version of my code. The exception sometimes shows up, sometimes not. Most of the time it happens when I'm trying to set UserAccountControl value. Does anyone know what could be the reason?

我发现当我处理创建 AdUser 的 DirectoryEntry 时会发生此错误,并且我仍在尝试使用 AdUser 对象.但是,上面发布的代码并非如此.DirectoryEntry 是否有可能以某种方式处置自己?

I found out that this error happens when I dispose DirectoryEntry the AdUser was created with and I'm still trying to use AdUser object. However this is not the case in the code posted above. Is it possible that DirectoryEntry somehow disposes itself?

当我尝试对许多活动目录对象执行操作时,我也会遇到此异常.例如,当我尝试为一千个用户设置 SecurityDescriptor 时,每 200-300 个用户就会出现此错误.当我在建立新连接后重试操作时,我没有遇到异常.消息是检测到raceonrcwcleanup.我的应用不是多线程的.

I also get this exception when I try to execute operation on many active directory objects. For example when I try to set SecurityDescriptor for one thousand users, I get this error every 200-300 users. When I retry operation after establishing new connections I don't get exception. The message is raceonrcwcleanup was detected. My app is not multithreaded.

任何帮助将不胜感激.

推荐答案

问题似乎是由 AdUser 中的 NativeObject 创建 DirectoryEntry 引起的.当我从以下位置更改 AdUser 时:

It seems that the problem is caused by creating DirectoryEntry from NativeObject in AdUser. When I changed AdUser from:

public class AdUser : DirectoryEntry 
{ 
public AdUser(DirectoryEntry entry) 
: base(entry.NativeObject) 
{ 
} 
} 

并创建了将 DirectoryEntry 视为组件的包装器:

And created wrapper that treats DirectoryEntry as a component:

public class ActiveDirectoryObject : IDisposable 
{ 
private bool disposed; 
public DirectoryEntry Entry { get; protected set; } 

public ActiveDirectoryObject(DirectoryEntry entry) 
{ 
Entry = entry; 
} 

public void CommitChanges() 
{ 
Entry.CommitChanges(); 
} 

public void Dispose() 
{ 
Dispose(true); 
GC.SuppressFinalize(this); 
} 

private void Dispose(bool disposing) 
{ 
if (!this.disposed) 
{ 
if (disposing) 
{ 
if (Entry != null) Entry.Dispose(); 
} 
disposed = true; 
} 
} 
} 

public class AdUser : ActiveDirectoryObject 
{ 
public AdUser(DirectoryEntry entry) 
: base(entry) 
{ 
} 
} 

然后我就没有收到这些错误信息.此处的更多详细信息:http://directoryprogramming.net/forums/thread/7171.aspx

Then I don't get these errors. Further details here: http://directoryprogramming.net/forums/thread/7171.aspx

这篇关于已经与其底层 RCW 分离的 COM 对象无法使用 - 为什么会发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:已经与其底层 RCW 分离的 COM 对象无法使用 - 为什

基础教程推荐