Freddy
Freddy

Reputation: 3274

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:

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?

Upvotes: 2

Views: 8203

Answers (3)

Youngjae
Youngjae

Reputation: 25050

For me, it occurrs when the test project runs as Any CPU mode. Add x86 or x64 for the test project in Configuration Manager.

If any of dependent library has x86 or x64, test project would need to follow either.

Upvotes: 1

tessafyi
tessafyi

Reputation: 2343

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:

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

If I right-clicked the name of the test in TestExplorer and ran the test: Image showing cursor hovering over "Run Selected Tests" in VS2015

or if I ran the test using CTRL+R T, then it happily ran. 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:

DisconnectedContext occurred

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'.

Additional information: Transition into COM context 0x6bd210 for this RuntimeCallableWrapper failed with the following error: The object invoked has disconnected from its clients. (Exception from 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. Releasing the interfaces from the current COM context (COM context 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.

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. 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. I'm not sure, though- I ended up just iterating through the List<int[]> and using MSTest CollectionAssert on each individual int[].

Upvotes: 1

JaredPar
JaredPar

Reputation: 754763

What appears to be happening here is the following

  • One of your components directly or indirectly uses a native COM object
  • The COM object is an STA object (IME most are)
  • STA COM objects essentially live on a thread
  • Your code is destroying that thread before the CLR can destroy the COM object

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.

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.

Upvotes: 3

Related Questions