I’m using LinqToActiveDirectory from codeplex and when I’m executing query using DirecotrySource I’m getting the following error:
DisconnectedContext was detected
Message: Context 0x3c5050′ is disconnected. Releasing the interfaces from the current context (context 0x3c4f98).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.
This occur only when debugging, if i set breakpoint on the last curly brackets of the method and press F10 to finish the execution.when running with no debug it’s working as expected.
Also, if I’m calling the GC.Collect() before the last curly brackets, i can safely exit.
I don’t want to call the GC as i know that it’s better to leave him to do his work.
I’ll be happy to get any idea about how to solve this isuue.
Thanks, Tamir.
You still have this problem in release mode and it will lead to a big crash or data corruption later on, but in debug mode it triggers the Managed Debugging Assistant that is trying to help you fix the issue: http://msdn.microsoft.com/en-us/library/2c1czate.aspx
In general the problem is that COM components are often owned by a specific thread (depending on how it supports being hosted and where it was created). The most common situation is that you created the context on a background thread or threadpool worker thread and now that thread has gone away (which kills the COM apartment or context, thus killing the COM component) and you later attempt to use the COM object.
Ultimately System.DirectoryServices uses ADSI which is a set of COM components. So are you passing stuff between threads, using Parallel LINQ, or anything like that? eg: If you are using lazy execution of LINQ on a background thread then passing IEnumerable to the foreground thread, the actual execution of the query will happen on the foreground thread… but if you stick a .ToList() in there, you will force evaluation on the background thread.
Please let us know if any of this helps and if you are using threading and we can provide more direction.