Webservice throws internal server error when webbrowser control is used
I have a web service, created using C#. For some reason, it seems to t开发者_运维知识库hrow a 500 internal server error whenever I invoke my exposed method. I set some breakpoints and found the following line is what does it:
m_Browser = new WebBrowser();
Any idea why this line would cause my service to crash? The WebBrowser control is in the System.Windows.Forms namespace.
That's probably because as the documentation clearly states it
The WebBrowser class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.
while a web service runs in a multi thread apartment mode (MTA). This same article shows a hack that could be used to run your web service in an STA mode but the performance of doing so might decrease.
Clearly a WebBrowser
control is not meant to be used in a web service but in a client application with a GUI. So usually when you are trying to use something into a scenario that this something is not meant for, there's a price to pay.
精彩评论