views:

369

answers:

1

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?

A: 

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.

JaredPar
Thanks, AFAIK I am not using any COM object. It is an asp .net application and most of the test just mock Model and Controller calls.
Freddy
@Freddy, it sounds like you are using one under the hood then as this error message is related to COM objects.
JaredPar