问题
After setting up a unit test in VS2105 which created some COM objects using Unity I started getting the following error:
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'.
I had a quick look to see if anyone else had the same problems and a lot of the solutions to the problem were either to fire off the test in it's own thread or change the target architecture to x64. Neither of these solutions felt quite right to me as they are more like work-arounds to the problem.
So after little thought I realised the problem is that the COM objects are not being given enough time by the test framework to clear down. So I came up with the following solution which worked.
回答1:
To fix the problem I added the following code to the tear down / test clean up method of the unit test:
_unity.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
The first line is only need if using Unity however the main part of fix is the last two lines. They force a garbage collection and then tell the current thread to wait until it has completed. Thus allowing the COM objects to be cleared down properly.
来源:https://stackoverflow.com/questions/37787161/managed-debugging-assistant-disconnectedcontext