Unable to create an object of com components written in unmanaged code from .Net wcf service

194 Views Asked by At

I am writing a Wcf Service library and want to load an unmanaged com dll. The com dll threaded model is not set so its a Single-threaded legacy component that runs only in the main STA threading model

In wcf service, I have set the apartment as STA like

Now when I try to create the instance of com component, it gives an exception

System.Runtime.InteropServices.SEHException : External component has thrown an exception.

When I debug the com dll and it was throwing an error RPC_E_WRONG_THREAD.

I can fix the problem by setting threading model to Free Threaded Marshaller. But i am not allowed to change the current threading model of com dll.

So please help me that how can I access the com dll from wcf service?

.Net Framework 4.0, Windows 7 x64

1

There are 1 best solutions below

0
On

Well, finally the problem is resolved....

steps:

  1. host the wcf service library into the WindowsForm App.
  2. set the WindowsForm app's threading apartment to STA
  3. set the dispatcher defined in the wcf service to the current dispatcher like

    WcfTest.Service1.disp = System.Windows.Threading.Dispatcher.CurrentDispatcher;

and thats all :)