If you're using WinForms/WPF objects from a thread that is not a single-thread apartment thread you'll get this exception. In order to use those objects from your WCF Service you need to create a new thread, set the apartment state on that thread to STA
and then start the thread.
My trivial example takes a string and checks it against a WPF TextBox's SpellCheck feature:
public bool ValidatePassword(string password)
{
bool isValid = false;
if (string.IsNullOrEmpty(password) == false)
{
Thread t = new Thread(() =>
{
System.Windows.Controls.TextBox tbWPFTextBox = new System.Windows.Controls.TextBox();
tbWPFTextBox.SpellCheck.IsEnabled = true;
tbWPFTextBox.Text = password;
tbWPFTextBox.GetNextSpellingErrorCharacterIndex(0, System.Windows.Documents.LogicalDirection.Forward);
int spellingErrorIndex = tbWPFTextBox.GetNextSpellingErrorCharacterIndex(0, System.Windows.Documents.LogicalDirection.Forward);
if (spellingErrorIndex == -1)
{
isValid = true;
}
else
{
isValid = false;
}
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
}
return isValid;
}
You can also reference this S.O. post:
How to make a WCF service STA (single-threaded)
As well as the link in the answer:
http://www.netfxharmonics.com/2009/07/Accessing-WPF-Generated-Images-Via-WCF