I had the problem described in this question: It is not possible to throw an exception to a COM object of the type that appears as an error:
Unable to pass a COM object of type "System .__ ComObject" to the interface, enter "IMyInterface". This operation failed because the QueryInterface call of the COM component for the interface with the IID error '{GUID}' due to the error: There is no support for such an interface
My WPF application calls the .NET library, which ultimately calls the COM object. This works great, but works on the main thread and blocks the user interface. When I create a new thread and call the library from it, I get this error. None of these solutions on another issue work for me. I am trying to understand how the runtime can load type information, but not share it between threads.
I understand that WPF applications are STAs, and I know that this means that any objects moving between threads will be combined by COM. I donβt understand how type information says "This is a COM object, and its GUID is this" may be in AppDomain, but not available for the second stream.
Where is the type information loaded? Is it in the AppDomain or in the stream? In any case, how can I relate thread type information? How can i fix this?
I read this:
http://www.codeproject.com/Articles/9190/Understanding-The-COM-Single-Threaded-Apartment-Pa
and this:
http://msdn.microsoft.com/en-us/library/ms973913.aspx#rfacomwalk_topic10
and a bunch of other things that talk about COM interoperability but didn't help me fix this.
As a Hans Passant answer, I create my library object in a second STA thread:
var thread = new Thread(delegate() { var aeroServer = new AeroServerWrapper(Config.ConnectionString); var ct = new CancellationToken(); aeroServer._server.MessageReceived += ServerMessageReceived; aeroServer.Go(@"M:\IT\Public\TestData\file.dat", ct); }); thread.SetApartmentState(ApartmentState.STA); thread.Start();
I understand that I may need to call Application.Run () to start the message queue if events do not fire, but I'm not so far away: it crashes as soon as it tries to create a COM object. AeroServerWrapper is located in a separate DLL, which calls the second DLL, which finally tries to create an instance of the COM object.
Any tips or articles would be greatly appreciated. I would like to solve this problem because it is: my plan B is to wrap the library in a console application, start the console application from the user interface and receive status messages from it via the named pipe. It will work, but it seems ugly.