I think the problem is that the ASP.NET worker process uses MTA threads,
which won't work with the WebBrowser. I had a similar problem in trying to
use the WebBrowser from a ThreadPool thread, which I solved by explicitly
creating a new thread, and then running the WebBrowser in that new thread.

Thread STAthread = new Thread(new ThreadStart(this.DoIEWork));

STAthread.ApartmentState = ApartmentState.STA;

STAthread.Start();


In the DoIEWork method, I created and used the WebBrowser control. NOTE the
web browser control is not thread safe! If you using this method, create a
static object to lock on, otherwise you'll have problems

private void DoIEWork() {

    lock (_someStaticLockObject) {

        System.Windows.Forms.Form frm = new System.Windows.Forms.Form();

        AxSHDocVw.AxWebBrowser wb = new AxSHDocVw.AxWebBrowser();



        ((System.ComponentModel.ISupportInitialize)(wb)).BeginInit();

        frm.Controls.Add(wb);

        ((System.ComponentModel.ISupportInitialize)(wb)).EndInit();



        ... do work here ...

        wb.Dispose();

    }

}


However, if all you are doing is getting the HTML, I would use the
HttpWebRequest and HttpWebResponse objects instead. They are much lighter,
faster, and you won't be downloading all the images, executing the
javascript, etc. I believe the HttpWebResponse handles redirects, but I
haven't confirmed this. Anyone?

Erick

----- Original Message -----
From: "Keyen" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, August 07, 2002 9:25 PM
Subject: [ADVANCED-DOTNET] shdocvw.dll and asp.net threading


Hi,

i'm trying to use shdocvw.dll (web browser activeX control). I successfully
imported the activeX control into a class library (contained in a
userControl).

i can reference this assembly in a vb.net project and perform any work need
with the activeX control.

my problem is that i cannot successfully use this assembly in asp.net. I
get the following message:

Could not instantiate ActiveX control '8856f961-340a-11d0-a96b-
00c04fd705a2' because the current thread is not in a single-threaded
apartment.

i've searched the internet and found that his mostly occurs when c#
programmers don't add the [ stathread ] attribute to their main() method
(vb.net programmers don't have this issue since vb.net  in it's great
wisdom does this for you)

Can anyone help me out?

btw:

i'm am trying to build an assembly can be used thru asp.net to 'browse' to
a web page and return the html, maybe there's an easier way that i haven't
found yet. The reason that i'm using the shdocvw.dll control is because
it'll also execute any client side code (such as redirects). any
suggestions would be appreciated.

thanks

You can read messages from the Advanced DOTNET archive, unsubscribe from
Advanced DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to