I believe the WebBrowser control is STA and a WCFservice hosted in a NT Service is MTA ? Thanks.
Right, this likely will not work. The WebBrowser control was meant to be used by a single STA thread. It won't map well to MTA in a web service, and will likely require some major hackery.
What are you trying to do? If you can describe your problem, we may be able to come up with an alternative solution.
edit
Ok, this is probably possible, although certainly hacky. Here's a theoretical implementation:
The code would look something like this:
public Bitmap GiveMeScreenshot()
{
var waiter = new ManualResetEvent();
Bitmap screenshot = null;
// Spin up an STA thread to do the web browser work:
var staThread = new Thread(() =>
{
var browser = new WebBrowser();
browser.DocumentCompleted += (sender, e) =>
{
screenshot = TakeScreenshotOfLoadedWebpage(browser);
waiter.Set(); // Signal the web service thread we're done.
}
browser.Navigate("http://www.google.com");
};
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
var timeout = TimeSpan.FromSeconds(30);
waiter.WaitOne(timeout); // Wait for the STA thread to finish.
return screenshot;
};
private Bitmap TakeScreenshotOfLoadedWebpage(WebBrowser browser)
{
// TakeScreenshot() doesn't exist. But you can do this using the DrawToDC method:
// http://msdn.microsoft.com/en-us/library/aa752273(VS.85).aspx
return browser.TakeScreenshot();
}
Also, a note from past experience: we've seen issues where the System.Windows.Forms.WebBrowser doesn't navigate unless it's added to a visual parent, e.g. a Form. Your mileage may vary. Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With