Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebBrowser control fails to trigger event handlers

In a particular scenario, a WebBrowser control fails to trigger a NavigationComplete or NavigationFailed event handler.

Here's what should happen:

  1. Native Windows Phone 8.1 Silverlight app instantiates a WebBrowser and provides it a url to load an authentication web page.
  2. WebBrowser control loads the url and renders the page.
  3. User inputs credentials and taps a submit button to POST the info to an auth service.
  4. Auth service validates the credentials and responds with a 302 (redirect).
  5. WebBrowser control follows the redirect.
  6. A NavigationComplete event handler is attached to the control. When it finds a url pattern that indicates the redirect succeeded it executes an additional method.
  7. The additional method pulls an openid token out of the redirect url query parameters.
  8. User is authenticated and happy.

What actually happens:

After the user submits their credentials at step #3 the WebBrowser renders a blank white page and doesn't trigger any event handlers. The code is listening for every navigation event handler provided by the WebBrowser control.

What I've tried:

  • Recreated the scenario as closely as I can with a test heroku server. The WebBrowser goes to a dummy credential input page with a POST submit button that hits heroku again; heroku responds with a 302 and the real redirect url. The WebBrowser works just fine O_o
  • Added onTap events to the WebBrowser that checks the current url/navigates to another page to see if the control is somehow broken after the blank page error. The WebBrowser responds correctly.
  • Ensured that Javascript is enabled on the control.
  • Used the exact same server side infrastructure with a Windows 8 tablet app that shares 90% of the same code but with a WebView control. The WebView works just fine.

Any suggestions? Has anyone experienced this control acting up like this? Does anyone know of a library that might solve this issue (commercial or open source). Does the Android bridge for Windows or the iOS bridge for Windows support windows phone 8?

like image 971
davehenry Avatar asked Nov 05 '15 05:11

davehenry


1 Answers

Dave,

The WebBrowser control is designed to work directly in UI threads and, by what you said above, it is possible that you may be running it in a non-UI thread, a situation where unexpected behavior should be... expected.

If that is the case, you might try to turn the thread into a Single Threaded Apartment (STA) thread, as the apatment model is fairly more adequate for COM componentes (such as ActiveX components, in which class WebBrowser falls) and you can do it as follows:

yourThread.SetApartmentState(ApartmentState.STA);

For more information on STA threads, please refer to this page.

Since the WebBrowser control is an AciveX component, the contents of this page is also of relevance and points towads the same issue.

Hope it helps. ;)

like image 113
Julio Marco A. Silva Avatar answered Oct 02 '22 03:10

Julio Marco A. Silva