已被分开其基础RCW COM对象不能使用 - 它为什么会发生? [英] COM object that has been separated from its underlying RCW can not be used - why does it happen?

查看:1642
本文介绍了已被分开其基础RCW COM对象不能使用 - 它为什么会发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时会出现以下情况例外: 已经分离与其基础RCW COM对象,不能使用

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

样品code:

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

这是我的code简化版本。唯一的例外有时会显示出来,有时没有。大多数时候,它的时候,我试图设置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?

我发现,当我处理的DirectoryEntry的ADUser便有与创造,我仍然尝试使用ADUser便有对象这个错误发生。然而,这不是在code张贴以上的情况。是否有可能以某种方式的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.

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

看来这个问题是由从NativeObject在ADUser便有创建的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对象不能使用 - 它为什么会发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆