简体   繁体   English

在VS 2008中运行单元测试项目时出现DisconnectedContext错误

[英]DisconnectedContext Error while running Unit Test project in VS 2008

I have a unit test project inside my solution. 我的解决方案中有一个单元测试项目。 I keep adding new unit tests and modifying old ones. 我一直在添加新的单元测试并修改旧的单元测试。 Some days ago, a message box keeps appearing when running my unit test project. 几天前,运行我的单元测试项目时,一个消息框一直出现。 The message box say: 消息框显示:

DisconnectedContext was detected
Message: Context 0x2aae50' is disconnected.  Releasing the interfaces from the current context
(context 0x2aad98).This may cause corruption or data loss. To avoid this problem, please 
ensure that all contexts/apartments stay alive until the applicationis completely done with 
the RuntimeCallableWrappers that represent COM components that liveinside them.

If I press 'OK' the unit test run is cancelled, if I press 'Continue' the tests are run as expected. 如果按“确定”,则取消单元测试运行;如果按“继续”,则按预期运行测试。 The tests are not affected by this error (at least I don't think so), but it is very annoying. 测试不受此错误的影响(至少我不这么认为),但这非常令人讨厌。 It used to work ok, so I don't if there is something I changed on the solution or project. 它过去可以正常工作,所以如果在解决方案或项目上我没有做任何更改,我不会这样做。

I checked information in MSDN about this error and it said it is caused by: 我在MSDN中检查了有关此错误的信息 ,它说这是由以下原因引起的:

The OLE apartment or context has been shut down when the CLR attempts to transition into it. 
This is most commonly caused by STA apartments being shut down before all the COM components 
owned by the apartment were completely released This can occur as a result of an explicit 
call from user code on an RCW or while the CLR itself is manipulating the COM component, for 
example when the CLR is releasing the COM component when the associated RCW has been garbage 
collected.

And the resolution is: 分辨率是:

To avoid this problem, ensure the thread that owns the STA does not terminate before the 
application has finished with all the objects that live in the apartment. The same applies 
to contexts; ensure contexts are not shut down before the application is completely finished 
with any COM components that live inside the context.

Based on this explanation (which honestly I don't fully understand) I have not idea why this is happening when running my Unit Test project. 基于这种解释(老实说,我不完全理解),我不知道为什么在运行我的单元测试项目时会发生这种情况。

So the question is How I could get rid of this error? 所以问题是我如何摆脱这个错误?

What appears to be happening here is the following 以下内容似乎正在发生

  • One of your components directly or indirectly uses a native COM object 您的组件之一直接或间接使用本机COM对象
  • The COM object is an STA object (IME most are) COM对象是STA对象(IME最多)
  • STA COM objects essentially live on a thread STA COM对象本质上存在于线程中
  • Your code is destroying that thread before the CLR can destroy the COM object 您的代码在CLR销毁COM对象之前销毁了该线程。

It's really hard to say what the fix is without understanding some details about your COM object. 如果不了解有关COM对象的一些细节,很难说出解决方案是什么。 Typically though this problem is caused because of a failure to pump messages on a thread using the COM object. 通常,尽管此问题是由于无法使用COM对象在线程上泵送消息而引起的。

In some scenarios you can fix this problem by using a combination of Application.DoEvents and GC.Collect to work around the issue. 某些情况下,可以通过结合使用Application.DoEvents和GC.Collect来解决此问题。 It can also help to run them in a loop. 它还有助于循环运行它们。 This may help fix your test problem but there still may be a serious architecture issue with your application. 这可能有助于解决测试问题,但应用程序仍然可能存在严重的体系结构问题。

I ran into a similar problem when trying to debug a unit test using MSTest in Visual Studio 2015. The MSTest CollectionsAssert was not cooperating for a List<int[]> I had, so I tried to include an NUnit CollectionsAssert within the MSTest unit test, like so: 当尝试在Visual Studio 2015中使用MSTest调试单元测试时,我遇到了类似的问题。MSTest CollectionsAssert与我拥有的List<int[]>不配合,因此我尝试在MSTest单元测试中包括NUnit CollectionsAssert。 ,就像这样:

NUnit.Framework.CollectionAssert.AreEqual(Expected, Actual);

If I right-clicked the name of the test in TestExplorer and ran the test: 如果我在TestExplorer中右键单击测试名称并运行测试: 该图显示了光标悬停在VS2015中的“运行选定的测试”上

or if I ran the test using CTRL+RT , then it happily ran. 或者,如果我使用CTRL+RT进行了测试,那么它会愉快地运行。 However, if I tried to step through and debug the test using CTRL+R CTRL+T then I would get the a similar error message right after executing the NUnit.Framework.CollectionsAssert line: 但是,如果我尝试单步调试并使用CTRL+R CTRL+T调试测试,则在执行NUnit.Framework.CollectionsAssert行之后,我将得到类似的错误消息:

DisconnectedContext occurred 发生DisconnectedContext

Managed Debugging Assistant 'DisconnectedContext' has detected a problem in 'C:\\PROGRAM FILES (X86)\\MICROSOFT VISUAL STUDIO 14.0\\COMMON7\\IDE\\COMMONEXTENSIONS\\MICROSOFT\\TESTWINDOW\\te.processhost.managed.exe'. 托管调试助手'DisconnectedContext'在'C:\\ PROGRAM FILES(X86)\\ MICROSOFT VISUAL STUDIO 14.0 \\ COMMON7 \\ IDE \\ COMMONEXTENSIONS \\ MICROSOFT \\ TESTWINDOW \\ te.processhost.managed.exe'中检测到问题。

Additional information: Transition into COM context 0x6bd210 for this RuntimeCallableWrapper failed with the following error: The object invoked has disconnected from its clients. 附加信息:将此RuntimeCallableWrapper转换到COM上下文0x6bd210失败,并出现以下错误:调用的对象已与其客户端断开连接。 (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED)). (来自HRESULT的异常:0x80010108(RPC_E_DISCONNECTED))。 This is typically because the COM context 0x6bd210 where this RuntimeCallableWrapper was created has been disconnected or it is busy doing something else. 这通常是因为创建此RuntimeCallableWrapper的COM上下文0x6bd210已断开连接,或者它正忙于执行其他操作。 Releasing the interfaces from the current COM context (COM context 0x6bcfe8). 从当前COM上下文(COM上下文0x6bcfe8)释放接口。 This may cause corruption or data loss. 这可能会导致损坏或数据丢失。 To avoid this problem, please ensure that all COM contexts/apartments/threads stay alive and are available for context transition, until the application is completely done with the RuntimeCallableWrappers that represents COM components that live inside them. 为避免此问题,请确保所有COM上下文/公寓/线程都保持活动状态并且可用于上下文转换,直到使用代表其中包含COM组件的RuntimeCallableWrappers完全完成应用程序为止。

This error showed up when all breakpoints were removed, so it's not purely an issue of trying to communicate with Visual Studio to step through. 删除所有断点后会出现此错误,因此,尝试与Visual Studio通信以逐步解决问题不只是一个问题。 My best guess is that somehow the COM object that handles running a test in debug mode is disrupted by an Assert statement from a different testing framework. 我最好的猜测是,以某种方式在调试模式下运行测试的COM对象被来自其他测试框架的Assert语句破坏了。 I'm not sure, though- I ended up just iterating through the List<int[]> and using MSTest CollectionAssert on each individual int[] . 不过,我不确定-我最终只是遍历List<int[]>并在每个int[]上使用MSTest CollectionAssert。

For me, it occurrs when the test project runs as Any CPU mode. 对我来说,当测试项目以Any CPU模式运行时,它就会发生。 Add x86 or x64 for the test project in Configuration Manager. 在配置管理器中为测试项目添加x86x64

If any of dependent library has x86 or x64 , test project would need to follow either. 如果任何依赖库具有x86x64 ,则测试项目将需要遵循其中一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM