I am creating WCF web services
that automatea internet explorer
. There are multiple web service calls that need to access the same instance of Internet Explorer
. However, since the WCF
services are hosted on IIS
all the calls to the web service are executed in session 0. Now to access the same instance of Internet Explorer
I use SHDocVw.InternetExplorer.HWND
property which returns the window handle of an Internet Explorer
instance. In below code when executed as a WCF
service on IIS 7
the window handles always return 0 due to session 0 isolation. Also, I am not able to hook back to the same IE
instance or loop through all the open IE
windows. I can enumerate the process list and find process ids for each IE
window open in session 0, but cannot cast a System.Diagnostics.Process
to SHDocVw.InternetExplorer
object.
Below is my code:
public int GetWhd()
{
InternetExplorer ie = new InternetExplorer();
ie.Visible = true;
return ie.HWND;
}
public int SetWhd(string whd)
{
int wh = Int32.Parse(whd);
InternetExplorer ie = null;
ShellWindows s = new ShellWindows();
foreach (SHDocVw.InternetExplorer ie1 in s)
{
try
{
if (ie1.HWND == wh)
{
ie = ie1;
break;
}
}
catch { return 2; }
}
if (ie != null) { ie.Navigate("www.google.com"); return 1; }
return 0;
}
Any help will be much appreciated.
It's difficult to fully escape isolation in IIS7, but here's what I did in a similar scenario: In IIS, go to Advanced Settings on your application pool, and set it to run as a windows user. Make sure you logged in with that user at least once (.net creates some undocumented folders). Set Load User Profile to True
In my case I was automating MS Office, so I had the following 2 additional steps (first might be applicable): C:\Windows\System32\config\systemprofile create Desktop folder, give it write permissions C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\Templates make sure Normal.dotm is there and it has write permissions
Next, change settings on the DCOM objects
Start -> run -> comexp.msc
Open
Component Services -> Computers -> My Computer -> DCOM Config
locate the Internet Explorer entry,Right-click -> Properties -> Identity Tab -> select The interactive user
Alternatively, if your use case allows, host the WCF application inside a WPF application, and if needed, you can go as far as hosting an Internet Explorer window inside the app, which will give you more control. The browser control API seems limited at first (intelli-sense wise) until you cast it to the appropriate type. I can post this if needed.
EDIT: Also take a look at http://support.microsoft.com/kb/555134