views:

1801

answers:

5

I have a WCF service which includes UI components, which forces me to be in STA mode.

How do I set the service behaviour to STA-mode?

A: 

I would investigate using the [STAThread] attribute to switch the threading model. e.g.

[STAThread]
static void Main()
{
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new Host() };
        ServiceBase.Run(ServicesToRun);
}

Description of the STAThread attribute

But I'm confused why you're using UI components in a web service at all. Can you explain a bit more about why you're trying to do this?

John Sibly
A: 

The service uses a reference to a wpf dll which opens a ui window(used as view port) for picture analycies. When the service is trying to create an instance of that item(inherits from window) it throws an exception: The calling thread must be an STA

add comments to answer, don't post another answer
spoon16
A: 

I'm doing something similar to you.

My solution was to route all calls through an STA thread queue. I used a threadsafe collection from the new parallel framework to queue up Actions I wanted to run on a STA thread. I then had X number of STA threads that continually checked the queue for new actions to execute.

Will
A: 

This was the solution that I found worked for me:

Running ASMX Web Services on STA Threads

Mark Perry
Except he says WCF right in the title. -1.
John Saunders
Although its the wrong answer, the article was an interesting read!
RichardOD
+4  A: 

Try this article (WCF STA Threads), provides very clear instructions(and code) for using WCF and STA Threads.

It explains how to create a WCF behaviour to allow WCF operations to run in a STA thread.

Daniel Pamich