Hi guys,
I'm writing an application that uses a COM library. I have a class library that wraps the calls to the COM library and adds some more functionality. Eventually this will run as a Windows Service. For now I test it using a Winforms test harness. When the class library is created by the test harness everything seems to be working OK. The problems start when I try to run it as a service. It is created OK and even the first call to the COM is OK. The COM object then raises an event that I handle and in response to the result in the event I call another function in the COM library. The function is invoked successfully in the case when I run it from the test harness but when running as a Service an exception is thrown:
System.InvalidCastException occurred Message="Unable to cast COM object of type '' to interface type ''. This operation failed because the QueryInterface call on the COM component for the interface with IID '{350ADD2A-18CB-4D9C-BE28-48D53F14E6FB}' failed due to the following error: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))."
I can see that there are threading issues. In the test harness case all this calls happen on the main thread and in the case of the Windows Service both the Service OnStart override and the COM event handler are on different threads. I've been trying different things without success. Any suggestions?
Rad.