Silverlight 4 – Take Advantage of Full Trust

I had the privilege of working with Silverlight 4 pre-beta bits in preparation for one of the PDC keynote demos. One of the coolest new features just announced for SL is support for running in “full trust” out of browser. This allows full access to the machine from within the Silverlight code – you can communicate with other software on the machine, hardware devices, etc. (The user has to accept the OOB dialog with some extra text warning of the full trust setting)


There is an XML file named OutOfBrowserSettings.xml that goes in the project properties folder. This file was first introduced for OOB in Silverlight 3. In SL4, there is one additional element:

    <SecuritySettings ElevatedPermissions=”Required/>

Visual Studio 2010 Beta 2 now has support for setting this value in the OOB properties dialog.


Detecting Full Trust Status

In the Silverlight application code, you can detect if you’re running full trust by inspecting a property on the Application object.

if ( !Application.Current.IsRunningOutOfBrowser )
  MessageBox.Show( “You are not running OOB” );
  else if ( !Application.Current.HasElevatedPermissions )
  MessageBox.Show( “You are not running in Full Trust” );

Now What Can I Do ?

One of the things we did for the keynote demo is to integrate the app with Microsoft Office. We create Excel spreadsheets, Word documents, Outlook meeting request, even check the Outlook calendar for available meeting times. There is no built-in support for Office in the Silverlight class library, so you have to resort to COM to talk to Office. Use the ComAutomationFactory.CreateObject() method to create a COM object from its ProgID, and you’re off and running. From there, it’s just working through the Office object model.

dynamic excel = ComAutomationFactory.CreateObject( "Excel.Application" );
excel.Visible = true;

dynamic workbook = excel.workbooks;

dynamic sheet = excel.ActiveSheet;

// create some headers and some data

for ( int i = 0; i < 3; i++ )
  dynamic cell = sheet.Cells[1, i];
  cell.Value = String.Format( "Column {0}", col );
  cell.Font.Bold = true;
// create some sample data (obviously, you can pull from whatever
// datasource you have to fill this in with good data)
for ( int row = 2; row < 10; row++ )
    for ( int col = 0; col < 3; i++ )
      dynamic cell = sheet.Cells[row, col];
      cell.Value = String.Format( "Row {0}, Col {1}", row, col );
  Marshal.ReleaseComObject( excel );


What Else?

With full trust, it’s almost unlimited what you can do from the Silverlight client code (local devices like cameras, scanners, bi-directional communication with COM objects, etc).

Party on!

Leave a Reply

Your email address will not be published. Required fields are marked *