DisconnectedContext错误而在VS 2008中运行单元测试项目 [英] DisconnectedContext Error while running Unit Test project in VS 2008

查看:297
本文介绍了DisconnectedContext错误而在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.

如果我preSS'OK'的单位试运行被取消,如果我preSS继续测试按预期运行。测试不受此错误(至少我不这么认为),但它是很烦人的。它曾经工作确定,所以我没有,如果有什么我的解决方案或项目发生变化。

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.

我查MSDN中有关这个错误以及该公司表示,的原因是:

I checked information in MSDN about this error and it said it is caused by:


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.

和分辨率是:


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


  • 一个直接或间接的组件使用本机COM对象

  • COM对象是STA对象(IME大部分是)

  • STA COM对象基本上是住在一个线程

  • 您$​​ C $ C被破坏之前,CLR线程可以摧毁COM对象

这真的很难说的解决办法是什么不理解你的COM对象的一些细节。通常虽然这个问题是因为未能对使用COM对象的线程抽取消息所造成的。

It's really hard to say what the fix is without understanding some details about your COM object. Typically though this problem is caused because of a failure to pump messages on a thread using the COM object.

部分方案中,可以使用的 Application.DoEvents 并GC.Collect的解决该问题。它也可以帮助在循环运行它们。这可能有助于解决您的测试问题,但仍有可能是一个严重的问题,架构与应用程序。

In some scenarios you can fix this problem by using a combination of Application.DoEvents and GC.Collect to work around the issue. 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.

这篇关于DisconnectedContext错误而在VS 2008中运行单元测试项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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