2

I am not using any thread in my application but I am getting this exception:

Additional information: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.

When I am Initializing WebBrowser in my web application.

WebBrowser wb = new WebBrowser();

This is my normal flow of calling this method.

Page_Load()
{
    TestWebBrowser("www.something.com")
}

static string TestWebBrowser(string url);
{
            WebBrowser wb = new WebBrowser();
            wb.ScrollBarsEnaenter code herebled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url);
            while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }

            mshtml.IHTMLDocument3 documentAsIHtmlDocument3 = (mshtml.IHTMLDocument3)wb.Document.DomDocument;

            var content = documentAsIHtmlDocument3.documentElement.innerHTML;
            return content ;
}

1 Answers1

0

Wrap the code in a Try/Catch block and insect the exception/inner exception for (hopefully) the reason why.

ChrisBint
  • 12,773
  • 6
  • 40
  • 62
  • This didn't work as well.. I looked into multiple solutions of adding [STAThread] before Main() if this is a console/window app and that worked fine but this technique didnt work for Page_Load. – Sidra Idrees Nov 18 '14 at 13:44
  • 1
    I suppose, You should remove DoEvents(). – icbytes Nov 18 '14 at 13:56
  • Bro! I am getting error in object initialization. So I think it wouldn't effect to remove DoEvents() – Sidra Idrees Nov 19 '14 at 04:47