Geeks With Blogs

News




What I do:

Identity Mine

MVVM Light

GalaSoft


What I am:

Microsoft Most Valuable Professional, Client Application Development

Microsoft Certified Technology Specialist, Windows Presentation Foundation

WPF disciples


Social:


View my profile on LinkedIn

XING
Creative Commons License
Diary of a Code Trotter by Laurent Bugnion is licensed under a Creative Commons Attribution 3.0 Unported License

All source code on this blog is licensed under the MIT license.

Copyright (c) 2006 - 2011 GalaSoft Laurent Bugnion

Post Categories

Laurent Bugnion (GalaSoft) Diary of a Code Trotter
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:
try
{
  System.Windows.Forms.Button bnOpenColorDialog
    = new System.Windows.Forms.Button();
  WindowsFormsHost oHost = new WindowsFormsHost();
  oHost.Child = bnOpenColorDialog;
  this.pnMainCanvas.Children.Add( oHost );
}
catch ( Exception ex )
{
  if ( BrowserInteropHelper.IsBrowserHosted )
  {
    System.Windows.MessageBox.Show( "XBAP cannot host WinForms controls" );
  }
  else
  {
    throw ex;
  }
}
More info about WPF security:
  • MSDN
  • Also, one of the most informative and condensed posts I ever read about WPF security
Posted on Wednesday, September 20, 2006 3:50 PM Technical stuff , .NET , WPF | Back to top


Comments on this post: Sandbox exceptions in XBAP: When are they really thrown?

# WPF Details
Requesting Gravatar...
* 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...
Left by on Sep 24, 2006 1:11 PM

Comments have been closed on this topic.
Copyright © Laurent Bugnion | Powered by: GeeksWithBlogs.net