A.I on the 1s and 0s

Alnur Ismail is a PM for Microsoft's XAML

Silverlight 4 Automation from A to Z

Silverlight 4 RC was released today. This post covers all you need to know about Silverlight 4 Automation.

Background

Automation (formerly called OLE Automation) is a COM-based technology that enables applications to exposes their unique and/or proprietary features to scripting tools and other applications. Consequently, by using Automation, developers can create custom applications that employ these features to automate frequent tasks.

Silverlight 4 RC

In Silverlight 4, a number of new classes have been added to the System.Runtime.InteropServices.Automation namespace to enable Automation. This allows Silverlight-based applications to play the role of an Automation client with the ability to directly access the programming model and services exposed by the Automation server.

This post will run through a simple example of how to 1) access the file system, 2) enumerate over a collection of files, and 3) handle an automation event.

Fundamentals

Before we get to the example, there are a few basic fundamentals that need to be called out:

  1. Automation is for Windows only
  2. Support for Automation revolves around late-bound COM -- there is no intellisense
  3. Automation can only be run in an Out of Browser application
  4. Automation requires Elevated Trust
  5. Production trusted applications run with current user privileges and cannot elevate to admin even if the user has admin privileges
    (Note: this does not apply when debugging)
  6. COM events that return values are not supported

Example

The first step for the Automation client is to access the Automation server. This is done by ensuring the server is available and then calling CreateObject with a programmatic identifier to get a reference to the server. Since we are accessing the file system we’ll use Scripting.FileSystemObject as the prog id.

  1. if (Application.Current.IsRunningOutOfBrowser)
  2. {
  3.     if (!AutomationFactory.IsAvailable)
  4.     {
  5.         MessageBox.Show("Automation not available.");
  6.         return;
  7.     }
  8.     dynamic fileSystem = AutomationFactory.CreateObject("Scripting.FileSystemObject");
  9. }

(Note: If you don’t check if the server is available a NotSupportedException will be thrown on platforms where COM is not supported)

(Note: If there was a failure to load, locate or return a reference to the specific prog id an Exception will be thrown)

Now that we have a reference to our server we can directly access it and get a reference to a directory:

  1. dynamic folder = fileSystem.GetFolder("c:\\program files");

One of the Automation features that wasn’t available in the beta was the ability to enumerate over an IEnumVARIANT collection returned from the server as a result, in this case, of a method call. With this support we can easily foreach to find all subfolders:

  1. foreach (dynamic subFolder in folder.SubFolders)
  2. {
  3.     ...
  4. }

To show an example of handling COM events, let’s pump this collection into an excel spreadsheet and notify the user before they close the workbook. In C# there are three ways to attach event handlers to Automation events. You can use the +=/-= syntax that requires the specified delegate to have the same signature as the Automation event signature. Secondly, you can handle the EventRaised event if you don’t know the Automation event signature. Finally, you can use the AddEventHandler/RemoveEventHandler methods that require knowledge of the event signature. I’ve chosen to use the EventRaised event:

  1. AutomationEvent beforeCloseEvent = AutomationFactory.GetEvent(workbook, "BeforeClose");
  2. beforeCloseEvent.EventRaised += (sender, e) =>
  3. {
  4.     MessageBox.Show("Closing workbook!");
  5. };

You can download a sample project here (VS2010 sln), and that’s the A to Z of Silverlight 4 Automation.

Comments

crysatl.ice said:

Can Silverlight Automation be run in an XCP Hostting application?

# May 22, 2010 10:09 AM

???Silverlight??????Windows 7????????? | IT News said:

Pingback from  ???Silverlight??????Windows 7????????? | IT News

# January 23, 2011 3:14 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)