Today, I spent the better part of my morning trying to troubleshoot a problem with COM interop within a WCF service. Here's the situation...
We currently have a legacy COM (VB6) component that creates a CSV file containing data needed to generate a PDF. Unfortunately, we don't have the time to re-write this component to .NET, so we have to make due for now.
I first tried the component within a standard WinForm, and I was able to create/invoke it with no problems. With this success, I proceeded to use said component within a WCF service so it could be exposed within our intranet (netTcpBinding). However, whenever the service received a request, the component would always hang during its "Initialize" method (this does nothing more that creates an Oracle session so it can start the retrieval of data).
Being a bit confused as the reason for the hanging, I started to check out whether the data being sent across the wire was valid. After realizing it wasn't the data, I decided to ask Google for some help. I was imediatelly attracted to this post on the MSDN Forums that tackled the same issue. How could I've not think of that?!? Of course! The WinForm ran as STA while the WCF service runs as MTA for their threads. So, I proceeded to create a new thread within my service and specified its apartment state to STA. Unfortunatelly, that didn't work. So I proceeded to get a bit creative with the problem.
I took the original WinForm application and made it run as MTA. After doing this both at the application and thread level (once again, by creating a new thread), I realized that it still worked correctly under the WinForm application. However, when I tried doing the same work-around within the WCF service, the request still hung. Arrgg!!
I came up with a messy hack to work around it (basically created an external COM host and ran the code within it) but I'm still not sure what the true issue is. Have any of you run into the same issue? If so, did you fix it?