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

<p>How do I set the service behaviour to STA-mode?</p>

<hr>

<p>The service uses a reference to a WPF DLL file which opens a UI window (used as view port) for picture analysis. When the service is trying to create an instance of that item (inherits from window) it throws an exception:</p>

<blockquote>
  <p>The calling thread must be an STA</p>
</blockquote>

[{'answer_id': 49448, 'author': 'John Sibly', 'author_id': 1078, 'author_profile': 'https://Stackoverflow.com/users/1078', 'pm_score': 0, 'selected': False, 'text': '<p>I would investigate using the [STAThread] attribute to switch the threading model.\ne.g.</p>\n\n<pre><code>[STAThread]\nstatic void Main()\n{\n        ServiceBase[] ServicesToRun;\n        ServicesToRun = new ServiceBase[] { new Host() };\n        ServiceBase.Run(ServicesToRun);\n}\n</code></pre>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.stathreadattribute.aspx" rel="nofollow noreferrer">Description of the STAThread attribute</a></p>\n\n<p>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?</p>\n'}, {'answer_id': 49671, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>I\'m doing something similar to you.</p>\n\n<p>My solution was to route all calls through an STA thread queue.  I used a threadsafe collection from the new <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=348F73FD-593D-4B3C-B055-694C50D2B0F3&amp;displaylang=en" rel="nofollow noreferrer">parallel framework</a> 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.  </p>\n'}, {'answer_id': 37119709, 'author': 'Matas Vaitkevicius', 'author_id': 1509764, 'author_profile': 'https://Stackoverflow.com/users/1509764', 'pm_score': 0, 'selected': False, 'text': '<p><a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute(v=vs.110).aspx" rel="nofollow">ServiceBehaviour</a> attribute allows you to specify behavior. In your case for single thread you would use following:</p>\n\n<pre><code>[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.PerCall)]\npublic class Service : IService\n{\n}\n</code></pre>\n\n<p>You <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.instancecontextmode(v=vs.110).aspx" rel="nofollow">might want to read about different</a> <code>InstanceContextMode</code>s to help you better choose on how you want service to behave.</p>\n\n<p>You also need to add to your <code>app.config</code> new service behavior (or edit existing one):  </p>\n\n<pre><code>    &lt;behavior name="wsSingleThreadServiceBehavior"&gt;\n      &lt;serviceThrottling maxConcurrentCalls="1"/&gt;\n    &lt;/behavior&gt;\n</code></pre>\n\n<p>and in your behavior configuration in same <code>app.config</code> set behaviorConfiguration like following:</p>\n\n<pre><code> &lt;service behaviorConfiguration="wsSingleThreadServiceBehavior" name="IService"&gt;\n    &lt;endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsEndpointBinding" name="ConveyancingEndpoint" contract="IService" /&gt;\n  &lt;/service&gt;\n</code></pre>\n\n<p>Hope this saves you some time</p>\n'}]
['https://Stackoverflow.com/questions/49445', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']