Sandbox exceptions in XBAP: When are they really thrown?

.NET, Technical stuff, WPF
See comments
Many features of WPF are not yet implemented for XBAP applications. For example, opening new windows is not possible when the WPF application runs in the browser host. Also, crossbow (integrating Windows Forms controls in WPF applications) fails when the WPF application runs as XBAP. When such sandbox-forbidden operations are executed, a security exception is thrown. Depending on the permission which is not granted, the type of the exception is different, but the mechanism is always the same.
When is the exception thrown exactly?
It is interesting to know when the security exception is thrown exactly, especially when you want to have one code base only for both the standalone and the XBAP scenarios. The idea here is to develop one code base only, and to compile it and deploy it differently depending on the platform you're targeting.
Thanks to Karen Corby's Flexible Application Template, it's as easy as switching configuration in Visual Studio. The Flexible Application Template defines two main ways to decide if the application currently running is XBAP or standalone:
  • Compile time: XBAP compiler switch (for example #if XBAP ... #else ... #endif)
  • Runtime: System.Windows.Interop.BrowserInteropHelper.IsBrowserHosted is true when the application is an XBAP
My tests indicate that the security exception is thrown as soon as (but not before) the method containing the faulty code is entered. If the said method is never executed, then everything runs OK. The faulty code itself must not be executed, but as soon as the containing method is entered, the exception is thrown.
For example:
protected void bnTest_Click( object sender, RoutedEventArgs e )
{
  if ( System.Windows.Interop.BrowserInteropHelper.IsBrowserHosted )
  {
    System.Windows.MessageBox.Show( "XBAP cannot host WinForms controls" );
  }
  else
  {
    System.Windows.Forms.Button bnOpenColorDialog
      = new System.Windows.Forms.Button();
    WindowsFormsHost oHost = new WindowsFormsHost();
    oHost.Child = bnOpenColorDialog;
    this.pnMainCanvas.Children.Add( oHost );
  }
}
If the button is clicked in XBAP mode, the application throws an exception even though the faulty code (instantiating the WindowsFormsHost) is never executed. So if you want the method to be executed in standalone AND in XBAP modes, you must do:
protected void bnTest_Click( object sender, RoutedEventArgs e )
{
#if XBAP
  System.Windows.MessageBox.Show( "XBAP cannot host WinForms controls" );
#else
  System.Windows.Forms.Button bnOpenColorDialog
    = new System.Windows.Forms.Button();
  WindowsFormsHost oHost = new WindowsFormsHost();
  oHost.Child = bnOpenColorDialog;
  this.pnMainCanvas.Children.Add( oHost );
#endif
}
Note that it's also no use to try and catch the exception. The following code fails too:
Previous entry | Next blog entry

Responses to “Sandbox exceptions in XBAP: When are they really thrown?”

  1. Says:

    * GeeksWithBlogs.Net discusses Sandbox exceptions in XBAP: When are they really thrown? ...when you want to have one code base only for both the standalone and the XBAP scenarios. The idea here is to develop one code base only, and to compile it and deploy it differently depending on the platform you're targeting...

Comments for Sandbox exceptions in XBAP: When are they really thrown?