problem with IE automation when used in webservice

  • Thread starter Thread starter Johny_W
  • Start date Start date
J

Johny_W

hi guys,

I have encountered a problem with IE automation (.NET 2.0, C#, mshtml,
SHDocVw). I am writing a class for manipulating specific html pages
with frames. It is supposed to be used from a web service. The
problem is that when I use the InternetExplorer object in web service,
it seems like the underlying IE doesn't support frames. When I try to
access HTMLDocumentClass.frames property I get the following exception:

Specified cast is not valid.
at System.RuntimeType.ForwardCallToInvokeMember(String memberName,
BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&
msgData)
at mshtml.HTMLDocumentClass.get_frames()
at myclass.mymethods()

the code lines, that cause the exception:

ie = new InternetExplorer();
object dummy = null;
ie.Navigate("myurl", ref dummy, ref dummy, ref dummy, ref dummy);
// ... omitted code assuring the html document is loaded ...
HTMLDocumentClass iedoc = (HTMLDocumentClass)ie.Document;
FramesCollection frames = iedoc.frames; // this throws the exception
....

(when the same code is used in a "normal" windows forms application, it
works fine).
 
Johny_W said:
hi guys,

I have encountered a problem with IE automation (.NET 2.0, C#, mshtml,
SHDocVw). I am writing a class for manipulating specific html pages
with frames. It is supposed to be used from a web service. The
problem is that when I use the InternetExplorer object in web service,
it seems like the underlying IE doesn't support frames. When I try to
access HTMLDocumentClass.frames property I get the following exception:

Specified cast is not valid.
at System.RuntimeType.ForwardCallToInvokeMember(String memberName,
BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&
msgData)
at mshtml.HTMLDocumentClass.get_frames()
at myclass.mymethods()

the code lines, that cause the exception:

ie = new InternetExplorer();
object dummy = null;
ie.Navigate("myurl", ref dummy, ref dummy, ref dummy, ref dummy);
// ... omitted code assuring the html document is loaded ...
HTMLDocumentClass iedoc = (HTMLDocumentClass)ie.Document;
FramesCollection frames = iedoc.frames; // this throws the exception
...

(when the same code is used in a "normal" windows forms application, it
works fine).

I'm having the same problem. Seems that all HTMLDocumentClass
properties that reference UI elements fail to get marshalled by COM
Interop when creating a new instance of the HTMLDocumentClass in web
services. Apparently web services execute according to a policy that
restricts the creation of UI elements. I'm trying to get at the
parentWindow property in order to be able to correctly release
resources when I'm done with the HTML parser but no go. When I attempt
to view the parentWindow property in the debugger the property
indicates that a Invalid Cast Exception has occurred. Thanks to this
foolishness I am frequently getting mshtml faults in w3wp.exe after
which time the web service becomes non functional. My guess is that
the windows are getting created but barring a reference to them in the
mshtml RCW wrapper there's not way to correctly release the reference
to the underlying COM object and it's associated resources.

The upshot seems to be that you can't create an HTMLDocumentClass
instance without creating several windows at the same time. You'll
notice that in a regular app the parentWindow is initialized correctly
and can be closed along with it's own parent window with a little
trickery. You'll also notice in task manager that memory and handles
get released correctly at the same time. Nevertheless in a web app or
web service it looks like you're screwed. Can't get to those windows.

If only Microsoft had modularized the HTML Parser in mshtml.dll and
made it independent of the UI we could all get a little rest.
 
OK. I got this to work. Thanks to a reply to another post.

I create a thread and set it's apartment state to STA. I execute the
parsing code in that thread and am able to access the associated
windows objects. I imagine this will for frames as well.
 
I tried your solution, but I get UnauthorizedAccessException
immediately when I'm creating InternetExplorer object.

details:
System.UnauthorizedAccessException: Retrieving the COM class factory
for component with CLSID {0002DF01-0000-0000-C000-000000000046} failed
due to the following error: 80070005. at MyMethod()

In web.config I have set impersonation to my administrator account and
also Full trust. Is it possible that the created thread has weaker
permissions?
 
Back
Top