0

I am attempting to use WCF to test my program. The problem I am running into is when I call methods through WCF, they are run on a worker thread. The method that I am trying to test needs to run in the UI thread or I get the following error:

DragDrop registration did not succeed. Current thread must be set to single thread apartment STA mode before OLE calls can be made. Ensure that your main function has STAThreadAttribute marked on it.

My main function in my program has the STAThread attribute. I was able to get it to work by doing the following inside of my method.

public void MyMethod(){
if (InvokeRequired) {
   Invoke(new MethodInvoker(MyMethod));
   return;
   }
   //Do stuff
}

I don't want to have to make this change for every method inside of my program. Is there a way to get WCF to run the methods on the UI thread everytime?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Dan Burgener
  • 796
  • 7
  • 16
  • Clear a few things up: this is about self-hosting in a WinForms/WPF app? You really do drag-and-drop through WCF? – H H Oct 11 '13 at 21:14
  • My program uses WinForms. The WCF is not "part" of my program. I am using WCF to test my program, by making calls to methods. I then call another method to verify that the previous method worked. For example, I first call AddPerson. The second call would be IsPersonInList, which would return a bool. – Dan Burgener Oct 11 '13 at 21:21
  • We are currently doing unit tests and coded UI tests, but have been asked to use WCF for further testing. – Dan Burgener Oct 11 '13 at 21:26
  • Well, the core answer is below. Maybe you can use WCF to steer the GUI tests, I don't know. – H H Oct 11 '13 at 21:30
  • Using UI Thread when running... WTF? :) – Nenad Oct 11 '13 at 21:32

1 Answers1

1

Is there a way to get WCF to run the methods on the UI thread everytime?

No. Even when you config WCF to run as SingleThreaded it will still be a server thread.

H H
  • 263,252
  • 30
  • 330
  • 514