none
C# - COM interoperability issues RRS feed

  • Question

  • I have to interface with an old legacy COM server, source code not available.  My VS C# solution can call this server successfully from a console application project, but if I try to make the same calls to the COM server from an HTTPHandler or a WCF service, both projects fail, at the same point.  They fail when I make the first call to COM, which is an instantiation (new) of a server object.  All of this is on the same host.  The WCF server, the HttpHandler and the Console App are all separate projects in the same solution, and they can call into common code housed in other projects in the sasme solution.  The reference to the COM dlls are through a common project they call into.

    Appreciate any thoughts on the matter.

    Monday, August 18, 2008 8:44 PM

Answers

  • A lot of COM components have apartment threading requirements, they need to be created on a thread in a single threaded apartment (STA).  Such a thread must be marked as STA and pump a message loop.  WCF and HTTP I/O completion threads most certainly don't meet this requirement.  There's no good fix for such a problem.  You can try creating a WF app and create the COM component on the UI thread.  Then use Control.Invoke() to marshal the method call to the UI thread.  The chance for deadlock is significant.
    Hans Passant.
    • Marked as answer by BBRiver Tuesday, August 19, 2008 3:06 PM
    Tuesday, August 19, 2008 12:15 PM
    Moderator

All replies

  • A lot of COM components have apartment threading requirements, they need to be created on a thread in a single threaded apartment (STA).  Such a thread must be marked as STA and pump a message loop.  WCF and HTTP I/O completion threads most certainly don't meet this requirement.  There's no good fix for such a problem.  You can try creating a WF app and create the COM component on the UI thread.  Then use Control.Invoke() to marshal the method call to the UI thread.  The chance for deadlock is significant.
    Hans Passant.
    • Marked as answer by BBRiver Tuesday, August 19, 2008 3:06 PM
    Tuesday, August 19, 2008 12:15 PM
    Moderator
  • Han,

    Thanks for the clear answer.  Rather than do as you suggest with a GUI based app, I can probably place the COM code in a console app on the server that will be a lightweight socket based server.  Then I'll just use socket calls, hopefully, to isolate the COM code.  Before I get too far down the line with this, do you think I could be a socket client from within an HTTPHandler or WCF process?

    Bob
    Tuesday, August 19, 2008 3:09 PM